Understanding Spawn Code

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,

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour
{
    public GameObject [] spawnpoints;
    public GameObject Enemy;



    void start ()
    {
        spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
        GameObject spawn = spawnpoints [Random.Range (4, spawnpoints.Length )];
        Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);

    }
       
}

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?

Ill try my best to Explain :stuck_out_tongue:

    public GameObject [] spawnpoints; // this is public, you can assign spawnpoints in the inspector
    public GameObject Enemy; // the enemy
        spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");

This line of code checks all objects on RunTime for the Tag “SpawnPoint” allowing you to define this later in the line of code

GameObject spawn = spawnpoints[Random.Range(4,spawnpoints.length)];

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

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

1 Like

was about to post but then i saw Rob’s. He basically covers everything

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?

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

1 Like

Well

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

1 Like

Woow lol, you beat me to it again

GameObject spawn = spawnpoints [Random.Range(4, spawnpoints.Length)];

if you have only got 3 elements in the array the parameters you’ve got going into the Random.Range are incorrect

… bah too slow :stuck_out_tongue: :slight_smile:

1 Like

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.

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour
{
    public GameObject [] spawnpoints;
    public GameObject Enemy;



    void start ()
    {
        spawnpoints = GameObject.FindGameObjectsWithTag("Spawnpoint");
        GameObject spawn = spawnpoints [Random.Range (0, spawnpoints.Length )];
        Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);

    }
       
}

Does anyone have any ideas?

Start

not

start

c# is case sensitive :slight_smile:

1 Like

Yup that was it:) Thanks you so much. Facepalm

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 :smile: