简体   繁体   English

Unity C# 空引用异常

[英]Unity C# Null Reference Exception

I am trying to get data from an int variable in Unity using C# code.我正在尝试使用 C# 代码从Unityint变量获取数据。 Below is the C# code I am using to get the int .下面是我用来获取intC#代码。

using UnityEngine;
using System.Collections;

public class endGameMessage : MonoBehaviour {
public static int score2;

void Start () {
    GameObject thePlayer = GameObject.FindWithTag("Player");
    gameScript game = thePlayer.GetComponent<gameScript>();
    score2 = game.score;
}

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

    Debug.Log (score2);

}
}

Below is the code from the other script I am trying to pull the data from.下面是我试图从中提取数据的另一个脚本中的代码。

using UnityEngine;
using System.Collections;

public class gameScript : MonoBehaviour {
//score
public int score = 0;
void OnTriggerEnter(Collider other) {
    if(other.gameObject.tag =="hammer"){
        GameObject.FindGameObjectWithTag("pickUpMessage").guiText.text = ("Picked Up A Hammer");    

        Destroy(other.gameObject);
        Debug.Log("collision detected hammer");
        audio.PlayOneShot(gotHit);
        score = score+10;
    }
     }
}

I can get the the int value to come across to the other script but its always 0 even if the int was meant to be 10.我可以让int值遇到另一个脚本,但它始终为 0,即使int是 10。

My question is how would i keep the value across the scripts?我的问题是如何在脚本中保持值? Any help is appreciated.任何帮助表示赞赏。

try this试试这个

public static int score2
{
    get
    {
        return GameObject.FindWithTag("Player").GetComponent<gameScript>().score;
    }
}

You have a lot of possibilities.你有很多可能性。

The first one is to set your Score as a static argument for you gameScript .第一个是将您的 Score 设置为 gameScript 的静态参数

  • So you can access it anywhere just like that :所以你可以像这样在任何地方访问它:

     int myScore = gameScript.Score ;
  • And the declaration should be :声明应该是:

     public static int score;

The second possibilities is far better if you want to save a lot of differents values from differents script.如果您想从不同的脚本中保存许多不同的值,第二种可能性要好得多。 In this case, you need to define a gameContext singleton.在这种情况下,您需要定义一个 gameContext 单例。

If you don't know what is this, you should take a look at singleton in C# : [ https://msdn.microsoft.com/en-us/library/ff650316.aspx]如果你不知道这是什么,你应该看看 C# 中的单例:[ https://msdn.microsoft.com/en-us/library/ff650316.aspx]

Singleton will allow you to have a single instance of your gameContext. Singleton 将允许您拥有游戏上下文的单个实例。 In your case, your singleton will have a Score attribute.在您的情况下,您的单身人士将有一个 Score 属性。 And you will be able to get the value from any scene and any scripts.您将能够从任何场景和任何脚本中获得价值。

This is the best way so far.这是目前最好的方法。

score2 is read once at start and then never again. score2在开始时读取一次,然后不再读取。 int is an integral type in C# and thus passed by value ie it receives a copy. int是 C# 中的整数类型,因此 按值传递 即它接收一个副本。 There several ways to solve this problem.有几种方法可以解决这个问题。

The easiest solution is to access the gameScript.score directly - it provides read/write access to everyone anyway.最简单的解决方案是直接访问gameScript.score - 它无论如何都为每个人提供读/写访问权限。 To encapsulate it you may choose to define a property .要封装它,您可以选择定义一个属性

A better way could be to define a new class GameStatus which holds all relevant things.更好的方法是定义一个新的类GameStatus来保存所有相关的东西。 This can be implemented as singleton for example.例如,这可以实现为例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM