Unity3D Null reference in Instantiate

2019-08-17 07:09发布

The "Object reference not set to an instance of an object" error is still showing.

I tried Debug.Log on every variable, no errors.

This is my code:

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

public class PlatformSpawn : MonoBehaviour {

    public GameObject platform;
    public GameObject life;
    private Vector3 platformrotation;
    private Vector2 platformpoint, lifepoint;
    private float platformrange, liferange;

    public List<Vector2> SpawnList = new List<Vector2> ();

    void Update () {
        float randposition = Random.Range (-10f, 10f); //x-axis
        platformrange -= 5; //y-axis
        float randrotation = Random.Range(-20f, 20f); //z-axis rotation
        liferange = platformrange + 1.56f;

        platformpoint = new Vector2 (randposition, platformrange);
        platformrotation = new Vector3 (0f, 0f, randrotation);
        lifepoint = new Vector2 (randposition, liferange);
        SpawnList.Add (platformpoint);

        GameObject Tlife = (GameObject)Instantiate (life, life.transform.position, life.transform.rotation);
        GameObject Tplatform = (GameObject)Instantiate (platform, platform.transform.position, platform.transform.rotation);

        Tlife.transform.position = lifepoint;
        Tplatform.transform.position = platformpoint;

        if (SpawnList.Count >= 10) {
            Tplatform.transform.rotation = Quaternion.Euler (platformrotation);
        }
    }
}

The error is in

GameObject Tlife = (GameObject)Instantiate (life, life.transform.position, life.transform.rotation);

Thanks 4 the help ^_^

P.S. The prefabs are still instantiating without any problems...

3条回答
来,给爷笑一个
2楼-- · 2019-08-17 07:39

I think some of you have'nt seen the last part,

P.S. The prefabs are still instantiating without any problems...

which is the question... the prefabs are still instantiating like there's no problem at all, but the catch is that the error is still "showing up"

My guess here that it is a bug from Unity3D, so i used Debug.Log(life) again and it shows that it is really null...

i have a prefab attached in the life variable and still working even the OnCollisionEnter2D...

I also found-ish a way to remove the error and still instantiate;

Private GameObject life;

void Start(){
life = new GameObject("name of the object");
//then attach some components for the new game object...
}

i tried it with a Collider2D and it works, i'm still finding a way to attach a SpriteRenderer with a Sprite on it...

But still, thank you for the help guys ^_^

查看更多
Melony?
3楼-- · 2019-08-17 07:41

life has to be a prefab that you've already created in the Editor and dragged/dropped onto the Inspector window for this object.

Ensure you:

  1. Created a prefab
  2. Selected this object in the Editor, so the Inspector appears
  3. Drag/dropped that prefab onto the "life" field in the Inspector
查看更多
【Aperson】
4楼-- · 2019-08-17 07:52

Don't use Instantiate(Object original, Vector3 position, Quaternion rotation) - use Instantiate(Object original)

In this case, you are trying to access the position and rotation of an object that doesn't yet exist (hence the null reference). You are trying to instantiate life and while instantiating you are telling it to "spawn" it at its position - which makes no sense.

Using Instantiate(Object original) will instantiate your object with the position and rotation specified in the prefab - which is what you are trying to do anyway.

Unfortunately, it seems Unity's docs don't really say much about the latter overload for Instantiate, and mainly explain how the former works: http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

EDIT: Also check you definitely assigned the prefab in the Inspector, otherwise you'll still have issues since life doesn't reference anything.

查看更多
登录 后发表回答