我一直在编写脚本的错误,我正在编写脚本工作,但我想修复错误

时间:2015-10-12 16:33:31

标签: c# unity3d scripting nullreferenceexception

我继续在游戏的得分计数器脚本上得到这个错误,我正在让得分计数器工作,但这个错误只是令人讨厌,我不想发布它与错误堆叠。

错误:

NullReferenceException: Object reference not set to an instance of an object
ScoreCounter.Update () (at Assets/ScoreCounter.cs:26)

脚本:

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

public class ScoreCounter : MonoBehaviour
{
public static int score;        // The player's score.


public Text text;                      // Reference to the Text component.

void Start ()
{
    // Set up the reference.
    //text = GetComponent <Text> ();

    // Reset the score.
    score = 0;
}


void Update ()
{
    // Set the displayed text to be the word "Score" followed by the score value.

    text.text = "Score:" + score;
}
}

1 个答案:

答案 0 :(得分:0)

从错误消息中可以清楚地看出,- (NSString *)HMACSHA1:(NSData *)data { NSParameterAssert(data); NSData *keyData = [@"SampleSecretKey012345678" dataUsingEncoding:NSUTF8StringEncoding]; NSMutableData *hMacOut = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA1, keyData.bytes, keyData.length, data.bytes, data.length, hMacOut.mutableBytes); /* Returns hexadecimal string of NSData. Empty string if data is empty. */ NSString *hexString = @""; if (data) { uint8_t *dataPointer = (uint8_t *)(hMacOut.bytes); for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { hexString = [hexString stringByAppendingFormat:@"%02x", dataPointer[i]]; } } return hexString; } 对象永远不会被实例化。

我不太了解你的用例,以推荐它应该被实例化的地方,但是在Start()里面看起来是个好地方。

干杯