Search Unity

Problem With Spawning Enemy Prefab?

Discussion in 'Scripting' started by JoeeyIreland, Jan 3, 2014.

  1. JoeeyIreland

    JoeeyIreland

    Joined:
    Jan 23, 2013
    Posts:
    39
    I have created a spawner that spawns clones of my enemy prefab the problem i face is that i have the enemy moving towards my object of choice but when they spawn they do not move towards the object. if i place them in the scene and apply the object for them to target they start moving towards it perfectly but they do not from the spawner here is what happens.

    LINK TO MY IMAGE OR LOOK AT ATTACHED FILES >>>> http://imgur.com/DxBZFQ1

    And here is my code for moving towards the chest

    Code (csharp):
    1. var target: Transform;
    2. var startTime;
    3. var timer1: int;
    4. var timingOn: boolean;
    5. var GetReadyButton: boolean;
    6. var font : Font;
    7.  
    8. function Start()
    9. {
    10.    
    11. }
    12.  
    13. function Update()
    14. {
    15.    if(Input.GetButton("StartWave"))
    16.    {
    17.       timingOn = true;
    18.       startTime = Time.time; //time starter
    19.      
    20.    }
    21.    
    22.    if (timingOn)
    23.    {
    24.       TimerStart();
    25.    }
    26.    
    27.    if(!target)
    28.    {
    29.  
    30.       target = GameObject.FindWithTag("Chest").Transform;
    31.       return;
    32.    }
    33. }
    34.  
    35. function OnGUI ()
    36. {
    37.    GUI.skin.label.font = font;
    38.    GUI.Label (Rect (400, 40, 100, 20),"Press G To Start"); //First Text to tell player to press g to start
    39.    
    40.    if(Input.GetButton("StartWave"))
    41.    {
    42.       GetReadyButton = true;
    43.    }
    44.    
    45.    if(GetReadyButton)
    46.    {
    47.       GUI.Label (Rect (415, 55, 100, 20),"10 Seconds");
    48.    }
    49. }
    50.  
    51. function TimerStart()
    52. {
    53.    timer1 = Time.time;  //Set time
    54.  
    55.    if(timer1 - startTime >= 10)
    56.    {
    57.       //Debug.Log ("Timer finished, spawn now");
    58.       GetComponent(NavMeshAgent).destination = target.position;
    59.       //target = GameObject.FindWithTag("Chest").Transform;
    60.      
    61.     }    
    62. }
    I Hope someone can fully solve this because i have been stuck on this for awhile now :/

    Thank you
     

    Attached Files:

  2. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Well, as far as I can tell, this seems to be the problem:
    Code (csharp):
    1.  
    2.    if(!target)
    3.  
    4.    {
    5.  
    6.       target = GameObject.FindWithTag("Chest").Transform;
    7.       return;
    8.  
    9.    }
    10.  
    Did you try to use debug.log or print to see if this if-statement runs through? It seems like it should, and I'm going to guess that the problem lies with the FindWithTag. Check if the Chest has the "Chest" tag.

    Also, try GameObject.Find("Chest") just in case. It may also just be "easier" and more specific, although it does have its disadvantages.
    http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html