简体   繁体   中英

Unity3D Null reference in Instantiate

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 ^_^

PS The prefabs are still instantiating without any problems...

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

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.

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

PS 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 ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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