ArgumentException:要实例化的内容为null。除了所有对象都被实例化

时间:2016-01-02 01:39:56

标签: c# unity3d

我收到此错误:

ArgumentException: The thing you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String 
message)
UnityEngine.Object.Instantiate (UnityEngine.Object original)
ScoreHandler.OnCollisionEnter (UnityEngine.Collision col) (at    
Assets/ScoreHandler.cs:92)

我的对象被命名为0,1,2,3,...和9.我将得分转换为字符串,然后使用相应的标题实例化游戏对象。

正如我所说,这一切都很完美。为什么我会收到此错误?

请注意,ScoreHandler是我脚本的名称。

问题在于我的所有对象都应该被实例化。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;


public class ScoreHandler : MonoBehaviour {
public int score = 0;
public List<GameObject> destroyList = new List<GameObject>();
public static GameObject[] Score;



// Use this for initialization
void Start () {
    score -= 80;

}

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

}





void OnCollisionEnter (Collision col)
{
    if (col.gameObject.name == "carA") {
        score += 10;


    }
    if(col.gameObject.name == "carB")
    {
        score += 10;


    }
    if(col.gameObject.name == "carC")
    {
        score += 10;

    }
    if(col.gameObject.name == "carD")
    {
        score += 10;


    }
    if(col.gameObject.name == "carE")
    {
        score += 10;


    }
    if(col.gameObject.name == "carF")
    {
        score += 10;


    }
    if(col.gameObject.name == "carG")
    {
        score += 10;


    }
    if(col.gameObject.name == "carH")
    {
        score += 10;

    }


    foreach (var go in destroyList)
    {
        Destroy(go);
    }
    destroyList.Clear();


    string scoreText = score.ToString ();


    Score = new GameObject[scoreText.Length];

    for (int i = 0; i < scoreText.Length; i++) {

        Score[i] = (GameObject)Instantiate (Resources.Load (scoreText 
[i].ToString ()));

        Score[i].layer = 8;
        Score[i].transform.localScale = new Vector3 (0.02F, 0.02F, 
0.02F);
        Score[i].transform.localPosition = new Vector3 (0.013F + i * 
0.01F, 0.12F, 0.0F);
        Score[i].transform.Rotate (0, 180, 0);

        destroyList.Add (Score[i]);



        }
    }

}

0 个答案:

没有答案