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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Understanding Spawn Code

Discussion in 'Scripting' started by Carter0, May 6, 2016.

  1. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    I have this code for spawning enemies in my space shooter game and the code works in MonoDevelop, but does not do what I want in Unity. This is probably a very simple and easy thing to figure out, but I am really new at coding and this stuff still poses a challenge to me.

    The code I have is partially mine, partially someone else's who was kind enough to help me on this forum, but the code he implemented is code I do not completely understand. Here is the code,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour
    5. {
    6.     public GameObject [] spawnpoints;
    7.     public GameObject Enemy;
    8.  
    9.  
    10.  
    11.     void start ()
    12.     {
    13.         spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
    14.         GameObject spawn = spawnpoints [Random.Range (4, spawnpoints.Length )];
    15.         Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);
    16.  
    17.     }
    18.        
    19. }
    The line GameObject spawn = spawnpoints [Random.Range (4, spawnpoints.Length )]; is the section I cannot grasp. I understand that I am setting a game object, spawn, equal to my array spawnpoints, but I don't understand anything about that section beyond this. Can someone explain this line to me and maybe tell me how to get this code actually working in Unity?
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Ill try my best to Explain :p

    Code (CSharp):
    1.     public GameObject [] spawnpoints; // this is public, you can assign spawnpoints in the inspector
    2.     public GameObject Enemy; // the enemy
    3.  

    Code (CSharp):
    1.  
    2.         spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
    3.    
    This line of code checks all objects on RunTime for the Tag "SpawnPoint" allowing you to define this later in the line of code

    Code (CSharp):
    1. GameObject spawn = spawnpoints[Random.Range(4,spawnpoints.length)];
    2.  
    this line sets a new GameObject named spawn to one of the Spawnpoints defined in the inspector, this is done by using Random.Range and SpawnPoints.length, .length gets the length of the array which is done by added objects to it in the inspector



    Code (CSharp):
    1. Instantiate(Enemy,spawn.transform.postion,spawn.transform.rotation);

    // this Spawns enemys, by instantiating a prefab, creating clones and sets there spawn position to the Transform and Rotation which is defined and stored in the Gameobject spawn
     
    Last edited: May 6, 2016
    Carter0 likes this.
  3. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    was about to post but then i saw Rob's. He basically covers everything
     
  4. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Thanks for the help! So I put the script on an empty game object and then set the size of the array to 3 in the inspector. After that I dragged three empty game objects into the array whose location matches where I want the enemy to spawn. But it still does not work.

    Here is a screenshot of Unity. What am I still doing wrong?
     

    Attached Files:

  5. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Code (CSharp):
    1. GameObject spawn = spawnpoints[Random.Range(4,spawnpoints.length)];

    On this code you are doing a random range between 4 and spawnpoints.length which is 3, so this would return null,

    Try changing 4 to 0
     
    Carter0 likes this.
  6. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Well
    Code (csharp):
    1. GameObject spawn = spawnpoints [Random.Range(4, spawnpoints.Length)];
    this line sets the minimum range to 4 and the maximum to the amount of spawnpoints you have, however though you only have 3. so change it to 1
     
    Carter0 likes this.
  7. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Woow lol, you beat me to it again
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Carter0 likes this.
  9. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Thanks once again! You guys are incredibly helpful! I changed that line of code to 0. Here is the proof. Unfortunately it still does not work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour
    5. {
    6.     public GameObject [] spawnpoints;
    7.     public GameObject Enemy;
    8.  
    9.  
    10.  
    11.     void start ()
    12.     {
    13.         spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
    14.         GameObject spawn = spawnpoints [Random.Range (0, spawnpoints.Length )];
    15.         Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);
    16.  
    17.     }
    18.        
    19. }


    Does anyone have any ideas?
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Start

    not

    start

    c# is case sensitive :)
     
    Carter0 likes this.
  11. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Yup that was it:) Thanks you so much. *Facepalm*
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    don't sweat it, I think that typo and the need for code tags on the forums are my two most common phrases on here lol :D