Search Unity

Spawning a Prefab after 50 seconds not working entirely correctly.

Discussion in 'Scripting' started by jayp246, Dec 2, 2016.

  1. jayp246

    jayp246

    Joined:
    Dec 2, 2016
    Posts:
    4
    This is my current Code for Spawning the prefab after 50 seconds the prefab spawns but the game crashes straight after.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BossSpawn : MonoBehaviour
    5. {
    6.     public GameObject Boss;
    7.     GameObject bossClone;
    8.  
    9.     void Start ()
    10.     {
    11.         StartCoroutine(WaitSpawner());
    12.     }
    13.  
    14.     IEnumerator WaitSpawner()
    15.     {
    16.         yield return new WaitForSeconds(50f);
    17.         Debug.Log("it has been 50sec");
    18.         bossClone = Instantiate(Boss, transform.position, Quaternion.identity) as GameObject;
    19.     }  
    20.  
    21.     void Update () {
    22.    
    23.     }
    24. }
    This is the Error I get
    UnassignedReferenceException: The variable Boss of BossSpawn has not been assigned.
    You probably need to assign the Boss variable of the BossSpawn script in the inspector.
    UnityEngine.Object.Internal_InstantiateSingle

    thank you for any help!
     
  2. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    Well the error is pretty explicit. you want to spawn whatever is in the Boss variable, but it is not assigned. so trying to spawn it crashes.
     
  3. jayp246

    jayp246

    Joined:
    Dec 2, 2016
    Posts:
    4
    Heya, thank you for the help! It is the first time I have coded so I am very much a beginner. When you say Assign do you mean assigning the Tag Boss to the Prefab? Or do you mean something else?
     
  4. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    In the editor, in the inspector panel you should have a field labeled boss. You need to drag a gameobject into that field.
     
  5. jayp246

    jayp246

    Joined:
    Dec 2, 2016
    Posts:
    4
    I have done that. The Boss spawns at 50 seconds the moment it spawns however the game crashes.
     

    Attached Files:

  6. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    In that case, could there be more than one bossspawn in your scene?

    A somewhat hackish fix would be to check if boss is properly assigned before attempting the spawn.

    If(boss != null)
    //Your instantiate code
     
  7. jayp246

    jayp246

    Joined:
    Dec 2, 2016
    Posts:
    4
    Thank you so much JC that got it to work. If it isn't any trouble could ask why that line of code worked?
     
  8. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    Like I said, it is hackish.
    What it does is validate if the attribute boss has been assigned before attempting to spawn it.
    The non-hack fix would be to figure out why you have a gameobject attempting to spawn a boss without the attribute being assigned.