Unity3D C# 无法从 class 创建对象
Unity3D C# cant create obj from class
我有一个脚本,其中写有两个 classes.I 从 class StatsTotal 中的第二个 class Stats 创建对象时遇到问题。stats[i] = new Stats(i);
我在这里从 Stats() 创建的 problem.The 对象为 null,因此数组 stats 的所有元素都为 null。在统一中我不能从我自己的 classes 创建对象?请help.Thanks.
StatsTotal
脚本:
public class StatsTotal : MonoBehaviour
{
// Use this for initialization
Stats[] stats;
static int carsCount;
int currentCar;
GameObject player;
void Start()
{
carsCount = PlayerPrefs.GetInt("CarsCount");
stats = new Stats[carsCount];
for (int i = 0; i < carsCount; i++)
{
stats[i] = new Stats(i);
if (stats[currentCar] != null)
{
Debug.Log(getStats().driftTimePresent + " " + getStats().driftTimeTotal);
}
stats[i].ReadTotal();
}
}
void OnLevelWasLoaded()
{
if (Application.loadedLevelName != "MenuScene" && Application.loadedLevelName != "PrizeScene")
{
foreach (GameObject obj in GameObject.FindGameObjectsWithTag("PlayerSystem"))
{
if (obj.GetComponent<PlayerScript>().mainPlayer)
{
player = obj;
break;
}
}
}
currentCar = GetComponent<VariableHandler>().selectedCar;
}
// Update is called once per frame
void Update()
{
if (Application.loadedLevelName != "MenuScene" && Application.loadedLevelName != "PrizeScene")
{
if (stats[currentCar] != null)
{
Debug.Log(getStats().driftTimePresent + " " + getStats().driftTimeTotal);
getStats().driftTimePresent = (int)player.GetComponent<PlayerScript>().driftTime;
}
}
}
public Stats getStats()
{
return stats[currentCar];
}
}
Stats
脚本:
public class Stats : MonoBehaviour
{
public int driftTimeTotal, driftTimePresent;
public int explodedTotal, explodedPresent;
public int flagsTotal, flagsPresent;
public int x2Total, x2Present;
public int shieldTotal, shieldPresent;
public int healthTotal, healthPresent;
public int scoreTotal, scorePresents;
public int finish1stTotal, finish1stPresent;
public int bonusesTotal, bonusesPresent;
public int hitsObjTotal, hitsObjPresent;
public int hitsCarTotal, hitsCarPresent;
public int jumpTotal, jumpPresent;
public int index;
public Stats(int index)
{
this.index = index;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ResetPresent()
{
driftTimePresent = 0;
explodedPresent = 0;
flagsPresent = 0;
x2Present = 0;
shieldPresent = 0;
healthPresent = 0;
scorePresents = 0;
finish1stPresent = 0;
bonusesPresent = 0;
hitsObjPresent = 0;
hitsCarPresent = 0;
jumpPresent = 0;
}
public void SaveTotal()
{
PlayerPrefs.SetInt("driftTimeTotal" + index, driftTimeTotal);
PlayerPrefs.SetInt("explodedTotal" + index, explodedTotal);
PlayerPrefs.SetInt("flagsTotal" + index, flagsTotal);
PlayerPrefs.SetInt("x2Total" + index, x2Total);
PlayerPrefs.SetInt("shieldTotal" + index, shieldTotal);
PlayerPrefs.SetInt("healthTotal" + index, healthTotal);
PlayerPrefs.SetInt("scoreTotal" + index, scoreTotal);
PlayerPrefs.SetInt("finish1stTotal" + index, finish1stTotal);
PlayerPrefs.SetInt("bonusesTotal" + index, bonusesTotal);
PlayerPrefs.SetInt("hitsObjTotal" + index, hitsObjTotal);
PlayerPrefs.SetInt("hitsCarTotal" + index, hitsCarTotal);
PlayerPrefs.SetInt("jumpTotal" + index, jumpTotal);
}
public void ReadTotal()
{
driftTimePresent = PlayerPrefs.GetInt("driftTimeTotal" + index);
explodedPresent = PlayerPrefs.GetInt("explodedPresent" + index);
flagsPresent = PlayerPrefs.GetInt("flagsPresent" + index);
x2Present = PlayerPrefs.GetInt("x2Present" + index);
shieldPresent = PlayerPrefs.GetInt("shieldPresent" + index);
healthPresent = PlayerPrefs.GetInt("healthPresent" + index);
scorePresents = PlayerPrefs.GetInt("scorePresents" + index);
finish1stPresent = PlayerPrefs.GetInt("finish1stPresent" + index);
bonusesPresent = PlayerPrefs.GetInt("bonusesPresent" + index);
hitsObjPresent = PlayerPrefs.GetInt("hitsObjPresent" + index);
hitsCarPresent = PlayerPrefs.GetInt("hitsCarPresent" + index);
jumpPresent = PlayerPrefs.GetInt("jumpPresent" + index);
}
public void SetTotal()
{
driftTimeTotal = PlayerPrefs.GetInt("driftTimeTotal" + index) + driftTimePresent;
explodedTotal = PlayerPrefs.GetInt("explodedTotal" + index) + explodedPresent;
flagsTotal = PlayerPrefs.GetInt("flagsTotal" + index) + flagsPresent;
x2Total = PlayerPrefs.GetInt("x2Total" + index) + x2Present;
shieldTotal = PlayerPrefs.GetInt("shieldTotal" + index) + shieldPresent;
healthTotal = PlayerPrefs.GetInt("healthTotal" + index) + healthPresent;
scoreTotal = PlayerPrefs.GetInt("scoreTotal" + index) + scorePresents;
finish1stTotal = PlayerPrefs.GetInt("finish1stTotal" + index) + finish1stPresent;
bonusesTotal = PlayerPrefs.GetInt("bonusesTotal" + index) + bonusesPresent;
hitsObjTotal = PlayerPrefs.GetInt("hitsObjTotal" + index) + hitsObjPresent;
hitsCarTotal = PlayerPrefs.GetInt("hitsCarTotal" + index) + hitsCarPresent;
jumpTotal = PlayerPrefs.GetInt("jumpTotal" + index) + jumpPresent;
}
}
只是不要在 Stats
class 中从 MonoBehaviour
inherit
。使用
class Stats {
......
}
不是
public class Stats : MonoBehaviour
{
}
如果继承自 MonoBehaviour
,则不能再使用 new
关键字创建脚本实例,必须使用 Instantiate 函数。
此外,您的保存和加载实现看起来一点也不好。由于您有许多变量要保存,只需将它们转换为 Json
,然后使用 PlayerPrefs
保存。要加载它们,请使用 PlayerPrefs
加载 json 数据,然后将其转换回 class
.
它应该是这样的:
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class Stats
{
public int driftTimeTotal, driftTimePresent;
public int explodedTotal, explodedPresent;
public int flagsTotal, flagsPresent;
public int x2Total, x2Present;
public int shieldTotal, shieldPresent;
public int healthTotal, healthPresent;
public int scoreTotal, scorePresents;
public int finish1stTotal, finish1stPresent;
public int bonusesTotal, bonusesPresent;
public int hitsObjTotal, hitsObjPresent;
public int hitsCarTotal, hitsCarPresent;
public int jumpTotal, jumpPresent;
public int index;
public void SaveTotal()
{
string playerJson = JsonUtility.ToJson(this);
PlayerPrefs.SetString("playerStats", playerJson);
}
public static Stats ReadTotal()
{
string playerJson = PlayerPrefs.GetString("playerStats");
if (playerJson == null)
{
return null;
}
Stats stats = JsonUtility.FromJson<Stats>(playerJson);
return stats;
}
}
用法:
//Create new Stats and Save it
Stats stats = new Stats();
stats.index = 90;
stats.SaveTotal();
//Load new Stats
Stats newStat = Stats.ReadTotal();
我有一个脚本,其中写有两个 classes.I 从 class StatsTotal 中的第二个 class Stats 创建对象时遇到问题。stats[i] = new Stats(i);
我在这里从 Stats() 创建的 problem.The 对象为 null,因此数组 stats 的所有元素都为 null。在统一中我不能从我自己的 classes 创建对象?请help.Thanks.
StatsTotal
脚本:
public class StatsTotal : MonoBehaviour
{
// Use this for initialization
Stats[] stats;
static int carsCount;
int currentCar;
GameObject player;
void Start()
{
carsCount = PlayerPrefs.GetInt("CarsCount");
stats = new Stats[carsCount];
for (int i = 0; i < carsCount; i++)
{
stats[i] = new Stats(i);
if (stats[currentCar] != null)
{
Debug.Log(getStats().driftTimePresent + " " + getStats().driftTimeTotal);
}
stats[i].ReadTotal();
}
}
void OnLevelWasLoaded()
{
if (Application.loadedLevelName != "MenuScene" && Application.loadedLevelName != "PrizeScene")
{
foreach (GameObject obj in GameObject.FindGameObjectsWithTag("PlayerSystem"))
{
if (obj.GetComponent<PlayerScript>().mainPlayer)
{
player = obj;
break;
}
}
}
currentCar = GetComponent<VariableHandler>().selectedCar;
}
// Update is called once per frame
void Update()
{
if (Application.loadedLevelName != "MenuScene" && Application.loadedLevelName != "PrizeScene")
{
if (stats[currentCar] != null)
{
Debug.Log(getStats().driftTimePresent + " " + getStats().driftTimeTotal);
getStats().driftTimePresent = (int)player.GetComponent<PlayerScript>().driftTime;
}
}
}
public Stats getStats()
{
return stats[currentCar];
}
}
Stats
脚本:
public class Stats : MonoBehaviour
{
public int driftTimeTotal, driftTimePresent;
public int explodedTotal, explodedPresent;
public int flagsTotal, flagsPresent;
public int x2Total, x2Present;
public int shieldTotal, shieldPresent;
public int healthTotal, healthPresent;
public int scoreTotal, scorePresents;
public int finish1stTotal, finish1stPresent;
public int bonusesTotal, bonusesPresent;
public int hitsObjTotal, hitsObjPresent;
public int hitsCarTotal, hitsCarPresent;
public int jumpTotal, jumpPresent;
public int index;
public Stats(int index)
{
this.index = index;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ResetPresent()
{
driftTimePresent = 0;
explodedPresent = 0;
flagsPresent = 0;
x2Present = 0;
shieldPresent = 0;
healthPresent = 0;
scorePresents = 0;
finish1stPresent = 0;
bonusesPresent = 0;
hitsObjPresent = 0;
hitsCarPresent = 0;
jumpPresent = 0;
}
public void SaveTotal()
{
PlayerPrefs.SetInt("driftTimeTotal" + index, driftTimeTotal);
PlayerPrefs.SetInt("explodedTotal" + index, explodedTotal);
PlayerPrefs.SetInt("flagsTotal" + index, flagsTotal);
PlayerPrefs.SetInt("x2Total" + index, x2Total);
PlayerPrefs.SetInt("shieldTotal" + index, shieldTotal);
PlayerPrefs.SetInt("healthTotal" + index, healthTotal);
PlayerPrefs.SetInt("scoreTotal" + index, scoreTotal);
PlayerPrefs.SetInt("finish1stTotal" + index, finish1stTotal);
PlayerPrefs.SetInt("bonusesTotal" + index, bonusesTotal);
PlayerPrefs.SetInt("hitsObjTotal" + index, hitsObjTotal);
PlayerPrefs.SetInt("hitsCarTotal" + index, hitsCarTotal);
PlayerPrefs.SetInt("jumpTotal" + index, jumpTotal);
}
public void ReadTotal()
{
driftTimePresent = PlayerPrefs.GetInt("driftTimeTotal" + index);
explodedPresent = PlayerPrefs.GetInt("explodedPresent" + index);
flagsPresent = PlayerPrefs.GetInt("flagsPresent" + index);
x2Present = PlayerPrefs.GetInt("x2Present" + index);
shieldPresent = PlayerPrefs.GetInt("shieldPresent" + index);
healthPresent = PlayerPrefs.GetInt("healthPresent" + index);
scorePresents = PlayerPrefs.GetInt("scorePresents" + index);
finish1stPresent = PlayerPrefs.GetInt("finish1stPresent" + index);
bonusesPresent = PlayerPrefs.GetInt("bonusesPresent" + index);
hitsObjPresent = PlayerPrefs.GetInt("hitsObjPresent" + index);
hitsCarPresent = PlayerPrefs.GetInt("hitsCarPresent" + index);
jumpPresent = PlayerPrefs.GetInt("jumpPresent" + index);
}
public void SetTotal()
{
driftTimeTotal = PlayerPrefs.GetInt("driftTimeTotal" + index) + driftTimePresent;
explodedTotal = PlayerPrefs.GetInt("explodedTotal" + index) + explodedPresent;
flagsTotal = PlayerPrefs.GetInt("flagsTotal" + index) + flagsPresent;
x2Total = PlayerPrefs.GetInt("x2Total" + index) + x2Present;
shieldTotal = PlayerPrefs.GetInt("shieldTotal" + index) + shieldPresent;
healthTotal = PlayerPrefs.GetInt("healthTotal" + index) + healthPresent;
scoreTotal = PlayerPrefs.GetInt("scoreTotal" + index) + scorePresents;
finish1stTotal = PlayerPrefs.GetInt("finish1stTotal" + index) + finish1stPresent;
bonusesTotal = PlayerPrefs.GetInt("bonusesTotal" + index) + bonusesPresent;
hitsObjTotal = PlayerPrefs.GetInt("hitsObjTotal" + index) + hitsObjPresent;
hitsCarTotal = PlayerPrefs.GetInt("hitsCarTotal" + index) + hitsCarPresent;
jumpTotal = PlayerPrefs.GetInt("jumpTotal" + index) + jumpPresent;
}
}
只是不要在 Stats
class 中从 MonoBehaviour
inherit
。使用
class Stats {
......
}
不是
public class Stats : MonoBehaviour
{
}
如果继承自 MonoBehaviour
,则不能再使用 new
关键字创建脚本实例,必须使用 Instantiate 函数。
此外,您的保存和加载实现看起来一点也不好。由于您有许多变量要保存,只需将它们转换为 Json
,然后使用 PlayerPrefs
保存。要加载它们,请使用 PlayerPrefs
加载 json 数据,然后将其转换回 class
.
它应该是这样的:
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class Stats
{
public int driftTimeTotal, driftTimePresent;
public int explodedTotal, explodedPresent;
public int flagsTotal, flagsPresent;
public int x2Total, x2Present;
public int shieldTotal, shieldPresent;
public int healthTotal, healthPresent;
public int scoreTotal, scorePresents;
public int finish1stTotal, finish1stPresent;
public int bonusesTotal, bonusesPresent;
public int hitsObjTotal, hitsObjPresent;
public int hitsCarTotal, hitsCarPresent;
public int jumpTotal, jumpPresent;
public int index;
public void SaveTotal()
{
string playerJson = JsonUtility.ToJson(this);
PlayerPrefs.SetString("playerStats", playerJson);
}
public static Stats ReadTotal()
{
string playerJson = PlayerPrefs.GetString("playerStats");
if (playerJson == null)
{
return null;
}
Stats stats = JsonUtility.FromJson<Stats>(playerJson);
return stats;
}
}
用法:
//Create new Stats and Save it
Stats stats = new Stats();
stats.index = 90;
stats.SaveTotal();
//Load new Stats
Stats newStat = Stats.ReadTotal();