Unity出错(C#) [英] Error with Unity ( C# )

查看:87
本文介绍了Unity出错(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的代码有问题,几乎当我按下创建按钮时我应该打印一些信息,但是就像我的代码找不到那些信息一样。

这是一个代码:

Hi guys, i have a problem with a code, practically when i press the button " create " i should print some information, but it's like my code cannot find those information.
Here's a code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CreateNewCharacter : MonoBehaviour {

	private BasePlayer newPlayer;
	private string playerName = "Enter Name";
	Text charText;
	private bool soldier = true;
	private bool pilot;
	private bool citizen;
	private bool create;


	// Use this for initialization

	void Awake(){

	
	}

	void Start () {

	}
	
	// Update is called once per frame
	void Update () {




	}


	void OnGUI()
	{
		create = GUI.Button(new Rect((Screen.width/2),(Screen.height/2)+25,100,20),"Create");
		playerName = GUI.TextArea(new Rect((Screen.width/2)-100,(Screen.height/2)-25,200,25),playerName ,15);//limited to 15 letters
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2),100,20),soldier, "Soldier")) {
			soldier = true;
			pilot = false;
			citizen = false;
		}
		
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2)+25,100,20),pilot, "Pilot")) {
			pilot = true;
			soldier = false;
			citizen = false;
		}
		
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2)+50,100,20),citizen, "Citizen")) {
			pilot = false;
			soldier = false;
			citizen = true;
		}

		if (create == true){
			Debug.Log("Button was pressed");
			if(soldier){
				newPlayer.PlayerClass = new BaseSoldierClass();
			}else if(pilot){
				newPlayer.PlayerClass = new BasePilotClass();
			}else if(citizen){
				newPlayer.PlayerClass = new BaseCitizenClass();
			}
			newPlayer.PlayerLevel = 1;
			newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
			newPlayer.Velocity = newPlayer.PlayerClass.Velocity;
			newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
			newPlayer.Strength = newPlayer.PlayerClass.Strength;
			newPlayer.Observation = newPlayer.PlayerClass.Observation;
			newPlayer.Luck = newPlayer.PlayerClass.Luck;
			newPlayer.PlayerName = playerName;
			
			//StoreNewPlayerInfo();
			//SaveInformation.SaveAllInformation();
			
			Debug.Log("Player Name : " + newPlayer.PlayerName);
			Debug.Log("Player Class : " + newPlayer.PlayerClass.CharacterClassName);
			Debug.Log("Player Level : " + newPlayer.PlayerLevel);
			Debug.Log("Player Stamina : " + newPlayer.Stamina);
			Debug.Log("Player Velocity : " + newPlayer.Velocity);
			Debug.Log("Player Intellect : " + newPlayer.Intellect);
			Debug.Log("Player Strength : " + newPlayer.Strength);
			Debug.Log("Player Observation : " + newPlayer.Observation);
			Debug.Log("Player Luck : " + newPlayer.Luck);
			Application.LoadLevel("Scene2");



			
		}




using UnityEngine;
using System.Collections;

public class BaseSoldierClass : BaseCharacterClass {
	
	public BaseSoldierClass() {
		CharacterClassName = "Soldato";
		CharacterClassDescription = "E' un soldato addestrato per combattere i gay";
		Stamina = 15;
		Velocity = 15;
		Strength = 20;
		Intellect = 10;
		Observation = 10;
		Luck = 10;
	}
	
}





有人知道如何解决这个问题吗?错误是这一个

NullReferenceException:对象引用未设置为对象的实例

CreateNewCharacter.OnGUI()(在Assets / Scripts / CreateNewCharacter.cs:61)



someone know how to fix it? the error is this one
NullReferenceException: Object reference not set to an instance of an object
CreateNewCharacter.OnGUI () (at Assets/Scripts/CreateNewCharacter.cs:61)

推荐答案

这意味着您需要初始化变量,而不是仅声明变量。这是新开发人员遇到的一个非常基本的错误。哪个对象抛出此异常,您需要为其赋值(初始化它),以便必须计算该值。



https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v = vs.110).aspx [ ^ ] MSDN有关于此异常的详细信息以及何时会被抛出。您需要阅读该文档,以便了解潜在的问题。



之前的一些帖子也可以帮助您:



如何解决NullReferenceException [ ^ ](见Sergey Alexandrovich Kryukov的回答)。

http:// stackoverflow .com / questions / 4660142 / what-is-a-nullreferenceexception-and-how-do-do-fix-it [ ^ ]



http://answers.unity3d.com/questio ns / topics / nullreferenceexception.html [ ^ ]关于Unity上类似异常的线程主题。
That means you need to initialize the variable other than declaring it only. It is a very basic error that new developers get into. Which so ever object throws this exception, you need to give it a value (initialize it), so that the value must be evaluated.

https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx[^] MSDN has a great information about this exception and when would it get thrown. You need to read that document so that you can understand the underlying problem.

A few more previous posts would also help you out:

How to resolve NullReferenceException[^] (See answer from Sergey Alexandrovich Kryukov).
http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it[^]

http://answers.unity3d.com/questions/topics/nullreferenceexception.html[^] A thread of threads about similar exceptions on Unity.


这篇关于Unity出错(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆