Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate as Child

Discussion in 'Scripting' started by Paykoman, Oct 24, 2016.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys, im instantiate a prefab as child object of my planet, the prefab instantiate normally but not as Child object and i get a error: NullReferenceException. This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class WaveSpawner : MonoBehaviour
    6. {
    7.     public Transform enemyPrefab;
    8.     public Transform spawnPoint;
    9.     public Text waveCountdownText;
    10.     public float timeBetweenWaves = 5f;
    11.  
    12.     private float countdown = 2f;
    13.     private int waveIndex = 0;
    14.  
    15.     void Update()
    16.     {
    17.         if (countdown <= 0)
    18.         {
    19.             StartCoroutine (SpawnWave ());
    20.             countdown = timeBetweenWaves;
    21.         }
    22.  
    23.         countdown -= Time.deltaTime;
    24.  
    25.         countdown = Mathf.Clamp (countdown, 0f, Mathf.Infinity);
    26.  
    27.         waveCountdownText.text = ("Next wave in: ") + string.Format("{0:00.00}", countdown);
    28.     }
    29.  
    30.     IEnumerator SpawnWave()
    31.     {
    32.         waveIndex++;  
    33.  
    34.         for (int i = 0; i < waveIndex; i++)
    35.         {
    36.             SpawnEnemy ();
    37.             yield return new WaitForSeconds (1.0f);
    38.         }
    39.  
    40.     }
    41.  
    42.     void SpawnEnemy()
    43.     {
    44.         GameObject go = Instantiate (enemyPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
    45.         go.transform.parent = GameObject.Find ("Planet").transform;
    46.     }
    47. }
    48.  
    Any help if possible, appreciate
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Assuming the error is on line 45 - there's no object in your scene named Planet.

    If it's always the same object then skip the lookup and just make a Transform variable and assign via Inspector.
     
  3. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Nothing in the code looks wrong. I'd start by making sure Find is returning something. You should minimize the number of times you call Find (poor performance) so you should probably put somethingl ike that in your Awake event anyway.

    Code (csharp):
    1. private GameObject planet;
    2.  
    3. void Awake()
    4. {
    5.     planet = GameObject.Find("Planet");
    6.     if(planet == null) Debug.Log("there's your problem");
    7. }
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The problem is
    exactly in line 45, but i hv a gameobject call Planet, that is an image

     
  5. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Actually, you can set parent to null, so not finding Planet wouldn't cause the error.
    That leaves your instantiated prefab.

    I bet either you'll get an error on line 44 or this will work (Unity's relationship between Object, GameObject, and transform is a little non-standard because of some of their C++ interop hacks):

    Code (csharp):
    1. GameObject go = (GameObject) Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
    The "as" keyword can return a null when it can't decide whether the type conversion is legal. You define enemyPrefab as a transform, but you're trying to use it as a GameObject. If you declare enemyPrefab as a GameObject then I would think either version of that line will work.

    I think there is also an overload of Instantiate that lets you assign the parent, too.
     
    Kiwasi likes this.
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It would if you attempt to access the transform of a null returned from Find - which OP is doing.

    Actually - your enemyPrefab is a Transform but you attempt to "as cast" it to GameObject as part of your Instantiate call, this fails and therefore your go variable is null.