0

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...

Calixt0
  • 1
  • 3
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dotnetom Jan 24 '16 at 07:58
  • 1
    You declare `life` in the posted code, but you never instantiate it as far as I can see, so when you pass in `life.transform.position` and `life.transform.rotation` to the method, you get the null reference exception, since a declared but un-instantiated object of a reference type is null. I expect that you would have the same error on `GameObject Tplatform = (GameObject)Instantiate(platform, platform.transform.position, platform.transform.rotation);` for the exact same reason. – Tim Jan 24 '16 at 07:59
  • How can i fix it? should i declare a new Vector2? – Calixt0 Jan 24 '16 at 08:04
  • I'm not familiar with Unity, but you can instantiate the two `GameObjects` like this: `GameObject life = new GameObject(); GameObject platform = new GameObject();`. However, that's a general answer based on C# principles - you should check the Unity documentation to see how to create a new GameObject, as it may require parameters or the class may be marked abstract, etc. – Tim Jan 24 '16 at 08:06
  • @Tim You're confusing things here. That's not the issue in this case; the instance of `life` will have been assigned inside the unity editor and is bound to an instance of a prefab at runtime. Creating a new `GameObject` doesn't achieve anything in this case. – Doug Jan 24 '16 at 13:44

3 Answers3

3

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
user3071284
  • 6,400
  • 6
  • 37
  • 51
Adam
  • 32,197
  • 16
  • 119
  • 146
0

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.

Seanharrs
  • 56
  • 1
  • 7
  • The version of Instantiate you use here will make no difference - if your prefab is null, both versions will fail, just in slightly different ways. – Adam Jan 24 '16 at 13:43
  • @Adam mm... I think the advice in this answer is sound... the error will not occur if you use the other version of `Instantiate`; calling Instantiate on a null value results in `ArgumentException: The thing you want to instantiate is null.`; which is a more meaningful error. There's no reason to use the position and rotation as shown in the question. – Doug Jan 24 '16 at 13:48
  • Instantiate is ONLY for creating clones of an existing object. If the object is missing, it won't work properly. Fixing the missing object will make the code work perfectly, and the Question will be solved; changing the kind of Instantiate you use will have no effect - no object will be cloned (since there's nothing to clone). Also: it's perfectly valid to set pos/rot to what they already are, and later tweak them to other values. Makes it very easy to experiment with - I've seen it done on many projects. – Adam Jan 24 '16 at 13:51
  • @Adam I don't really see the point in your comments. If you're instantiating a prefab exactly as it has been prefabricated, why waste time and space adding two extra arguments that aren't required. I also mentioned that a null prefab would still cause issues. afaik there's nothing objectively wrong with the suggestion, I suppose it's a matter of opinion whether to add the extra two arguments or not. I do see your points though. – Seanharrs Jan 25 '16 at 09:04
0

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

Calixt0
  • 1
  • 3
  • The prefabs CANNOT be instantiating if life is null. Literally. By definition. Simply not possible - not least because the error you quoted will CANCEL the instantiate. You are either misinterpreting what you're seeing, or you're doing something else that's instantiating a prefab and tricks you into thinking this code has had some effect. PS: better to modify the question (use Edit link) rather than add an answer – Adam Jan 26 '16 at 16:03