- Home /
 
C# Object reference not set to an instance of an object right after GameObject.Instantiate
Hi guys, im pretty new to c# and unity, so it may be a stupid question.
Basically i'm trying to Instantiate 3 (or a variable amount) of game object from a prefab (wich is bound in unity onto script on other game object) somewhere "on the edge of the screen". However, when i try to acces the newObject to change its name i get that error mentioned in the title.
     using System;
     using UnityEngine;
     
     namespace Assets.Code.States
     {
         public class Generator
         {
             private References refInst;
             private Vector3 goThere;
             private Transform objectPrefab;
             
             
             public Generator ()
             {
             }
             
             public void CreateObjects()
             {
                 refInst = GameObject.Find ("GameManager").GetComponent<References>();
                 objectPrefab = refInst.bigObjectPrefab1;
     
                 int amount = 3;
                 
                 for (int i = 0; i < amount; i++)
                 {
                     float whereDoIGo = UnityEngine.Random.value * 4;
                     
                     if (whereDoIGo <= 1)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     
                     if (whereDoIGo > 1 && whereDoIGo <= 2)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     if (whereDoIGo > 2 && whereDoIGo <= 3)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     if (whereDoIGo > 3 && whereDoIGo <= 4)
                     {    
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(0.01f, UnityEngine.Random.value, 10));
                     }
                     
                     GameObject objectNew = GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity) as GameObject;
 
                     objectNew.name = "Hi there! " + i;
                     
                 }
                 
             }
         }
     }
     
 
 
              Are you sure that objectPrefab is not null? To make sure you may want to add "Debug.Log("objectPrefab.name="+objectPrefab.name);" line before Instantiate
Yes, tried it now just to be sure, its not null. All 3 objects get instantiated fine if i leave out objectNew.name = ....; or some other acces to the newly created object like position etc. 
Which line is throwing the error?
Try Debug.Log (objectNew);
If this is fine run the same thing on goThere and objectPrefab
Answer by Atro · Jun 13, 2014 at 11:10 AM
Ok, thx guys, got it fixed. BoredMoron ( nice nick dude ^^ ) pitched a nice idea, objectNew was indeed returning as null.
Thx to the answer here i figured out that "as GameObject" cast actually hides an error and returns null and by "hardcasting" GameObject objectNew = (GameObject) GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity); i get "InvalidCastException: Cannot cast from source type to destination type" wich "as" cast was actually hiding. 
So i fixed it by changing instantiate into Transform objectNew = (Transform)GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity);
Im bit dazzled by this voodoo but everything seems to work as a charm now.
Thx for ur help.
Your answer