Search Unity

spawning 1 object a certain amount of time in start function

Discussion in 'Getting Started' started by BrewNCode, Dec 2, 2018.

  1. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    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:
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     #region Variables
    4.     public GameObject[] creatures = new GameObject[3];
    5.     #endregion
    6.  
    7.     private void Start()
    8.     {
    9.         Instantiate(creatures[Random.Range(0, 3)], transform.position, Quaternion.identity);
    10.  
    11.     }
    12.  
    13.  
    14. } // 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?
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    I don't know, maybe...
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     #region Variables
    4.     public GameObject[] creatures = new GameObject[3];
    5.     public GameObject[] spawns = new GameObject[3]; // Empty gameObjects spawn points in scene
    6.     #endregion
    7.     private void Start()
    8.     {
    9.         Instantiate(creatures[0], spawns[0].position, Quaternion.identity);
    10.         Instantiate(creatures[1], spawns[1].position, Quaternion.identity);
    11.         Instantiate(creatures[3], spawns[2].position, Quaternion.identity);
    12.     }
    13. } // main class
     
  3. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    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.
     
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Or maybe..
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     #region Variables
    4.     public GameObject[] creatures = new GameObject[3];
    5.     public GameObject[] spawns = new GameObject[3]; // Empty gameObjects spawn points in scene
    6.     #endregion
    7.     private void Start()
    8.     {
    9.         Instantiate(creatures[0], spawns[0].position, Quaternion.identity);
    10.         Instantiate(creatures[1], spawns[1].position, Quaternion.identity);
    11.         if (Random.Range(0, 100) >= 50 ) // Don't spawn 3rd if less than 50
    12.         {
    13.              Instantiate(creatures[3], spawns[2].position, Quaternion.identity);
    14.         }
    15.     }
    16. } // main class
     
  5. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    Code (CSharp):
    1. IndexOutOfRangeException: Array index is out of range.
    2. GameManager.Start () (at Assets/Scripts/GameManager.cs:17)
    3.  
    D: The error is in the if statement.
     
  6. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    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'.
     
  7. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    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.
     
  8. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    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.
     
  9. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    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 ;)