spawning 1 object a certain amount of time in start function

So, I wanted to create a simple script that makes me instance no more than 3 instances of the same Game object every time. Meaning that sometimes it get installed 2 times, other gets 3 times instanced. (everything called in the Start function).

So, I have 3 spheres, Sphere 1, 2 and 3. With my code:

public class GameManager : MonoBehaviour
{
    #region Variables
    public GameObject[] creatures = new GameObject[3];
    #endregion

    private void Start()
    {
        Instantiate(creatures[Random.Range(0, 3)], transform.position, Quaternion.identity);

    }


} // main class

Only one of each balls gets instantiated in the Scene. How can I make the 3 or at least 2 instantiated in the Scene?

I don’t know, maybe…

public class GameManager : MonoBehaviour
{
    #region Variables
    public GameObject[] creatures = new GameObject[3];
    public GameObject[] spawns = new GameObject[3]; // Empty gameObjects spawn points in scene
    #endregion
    private void Start()
    {
        Instantiate(creatures[0], spawns[0].position, Quaternion.identity);
        Instantiate(creatures[1], spawns[1].position, Quaternion.identity);
        Instantiate(creatures[3], spawns[2].position, Quaternion.identity);
    }
} // main class

Nope, I would have done that myself if I wanted to spawn the 3 object at the same time. What I meant is having an interval of 2 to 3 clones per call.

Or maybe…

public class GameManager : MonoBehaviour
{
    #region Variables
    public GameObject[] creatures = new GameObject[3];
    public GameObject[] spawns = new GameObject[3]; // Empty gameObjects spawn points in scene
    #endregion
    private void Start()
    {
        Instantiate(creatures[0], spawns[0].position, Quaternion.identity);
        Instantiate(creatures[1], spawns[1].position, Quaternion.identity);
        if (Random.Range(0, 100) >= 50 ) // Don't spawn 3rd if less than 50
        {
             Instantiate(creatures[3], spawns[2].position, Quaternion.identity);
        }
    }
} // main class
IndexOutOfRangeException: Array index is out of range.
GameManager.Start () (at Assets/Scripts/GameManager.cs:17)

D: The error is in the if statement.

Change [Instantiate(creatures[3], spawns[2].position, Quaternion.identity);] to [Instantiate(creatures[2], spawns[2].position, Quaternion.identity);]

If you’re just coping and pasting code, you’re not learning anything. If you can’t or would’t learn to debug code you might as well take up something else. The error statement you provided said what and where the problem was. IndexOutOfRangeException in itself is pretty clear, but it provides more by telling you an Array index is out of range. Furthermore it tells you that the error is on line 17. Arrays are zero based so creatures[3] is out of range. That was my mistake, so you just copied my error. What I provided was off the top of my head and not tested against a compiler. There is also another error and I’ll let you find it (them).

The questions you asked in this post and in previous ones show you don’t understand the basics of coding. There are many ways to do what you asked and I provided the most simple example there is. You should have been able to that much on your own. You need to learn to code, maybe take a class online or something. I hate to resurrect program flow charts from the grave, but google that and try to work out what you want on paper first. Coding is nothing more than problem solving and debugging. Focus on learning those.

Don’t mean to be harsh, just sayin’.

yeah, I know what it means, I wanted to know why you would post that solution if it has an error. I just increased the array length and everything was solved.

Then why didn’t you just fix it? BTW you fix is not correct. Increasing the size of the array will get rid of the error but it will not work properly. You continue to prove my point from my last post. I sincerely apologize for not posting error free examples off the top of my head for you to copy and paste. I wouldn’t make that mistake again.

Actually, it did work. CONGRATULATION!

and Thank you

And yeah, if you could give error free examples that would be great. btw, it is transform.position, not just position :wink: