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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Using Instantiate.

Discussion in 'Scripting' started by Hiktses, Jun 19, 2020.

  1. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    I want my script to only Instantiate one game object.Can you guys help?
    Instantiate(boss,transform.position + transform.forward * 1.0f,transform.rotation);
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    This line of code instantiates only 1 gameobject of the asset called "boss", so probably your problem is somewhere else.
     
    Hiktses likes this.
  3. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    Code (CSharp):
    1. if(score.text == "Score:50")
    2.         {
    3.             Vector2 spawnPos = GameObject.FindWithTag("Player").transform.position;
    4.  
    5.             Instantiate(boss,transform.position + transform.forward * 1.0f,transform.rotation);
    6.  
    7.             spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
    8.  
    9.  
    10.  
    11.             Debug.Log("Spawned");
    12.         }
    Sorry,I Forgot to mention the whole "If".
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I'm guessing you have this code inside your Update() method or something. If so, it's going to run every single frame.
     
    Last edited: Jun 19, 2020
    Hiktses likes this.
  5. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    I managed to solve this problem with a bool.Sometimes I feel stupid.Thanks anyway.
     
  6. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I assume you want this, but this only works if score.text equals to "Score:50"

    Code (CSharp):
    1. if (score.text == "Score:50")
    2.         {
    3.             GameObject thePlayer = GameObject.FindWithTag("Player"); // you should do this here already in the Start() method
    4.             Vector2 spawnPos = thePlayer.transform.position;
    5.             spawnPos += Random.insideUnitCircle.normalized * spawnRadius; // random position of the boss
    6.             Gameobject myBoss = Instantiate(boss, spawnPos, Quaternion.identity); // spawnes the boss
    7.             myBoss.transform.LookAt(thePlayer.transform); // let the boss look to the player
    8.  
    9.             Debug.Log("Spawned: " + myBoss.name);
    10.         }
    11.  
     
    Hiktses likes this.