Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

''Random spawn'' problem

Discussion in 'Scripting' started by Jip1912, Aug 31, 2015.

  1. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hi,

    I got this problem that when I fill in coordinates at my array, the random spawn object doesn't spawn at the coordinates I filled in.

    GIF:
    https://gyazo.com/945bd80510ae145ab54da6a529e60dd9

    I would normally use 3 elements so that a line spawns at 1 of the 3 possible lines.

    I use this code for ''Random Spawn'' :

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class RandomSpawn : MonoBehaviour {

    public Vector3[] positions;

    void Start ()

    {
    int randomNumber = Random.Range (0, positions.Length);
    transform.position = positions[randomNumber];

    }

    }


    The code works, the only problem is that the item doesn't spawn at the coordinates I filled in.

    Thanks for helping.
     
  2. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    1. Use [code ][/ code] tags (without the spaces). It makes your code waaaaaay easier to read for us.
    2. Try printing the value you chose to the console. Maybe that helps you finding your problem.
    3. Don't use gifs like that. The poor resolution makes reading anything a pain.
    4. You seem to be getting an index out of bound exception from what I can tell on your gif.
     
  3. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    What is an index out of bound exception?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    google is your friend...

    I dont think it does, there is a bright read (totally unreadable) error in the console line... what does that say?
     
  5. Moosetaco

    Moosetaco

    Joined:
    Jan 27, 2013
    Posts:
    77
    its an error regarding your array

    positions.Length might be were you are getting issues.
    Say you have 5 vector3's loaded into positions... the length is 5 however if you were to try to assign:
    transform.position = positions[5];
    you would get an error.
     
  6. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    An array is a set of variables that the computer stores as one solid block in memory. Each # is an integer for example:
    Code (csharp):
    1. int[] fiveIntegers= new int[5];
    These 5 integers look like this in memory:
    ....[Random stuff].....[int0][int1][int2][int3][int4]....[Random stuff]....

    One way to access the individual integers (and afaik the only way in C#) is through indices:
    Code (csharp):
    1. fiveIntegers[0]; // gives you the first element (int0 above)
    2. fiveIntegers[1]; // gives you the second element (int1 above)
    3. ...
    4. fiveIntegers[n]; // gives you the (n-1)-th element
    An IndexOutOfBoundsException is thrown whenever you access an element that is outside the array. Say you access the 6-th element of an array that only contains 5.

    I hope this helps.
     
  7. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Random.Range(min, max) includes min, but excludes max.
    Random.Range(0, arrayOfSizeFive.Length) will return the numbers 0, 1, 2, 3 or 4.
     
  8. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I understand what you are saying, but it's not the problem. I got that error because all my lines had that Random Spawn Script as component. I don't know how or why it was there. I removed it at all lines and I don't get the error anymore. But still, the line goes to the wrong positions. Btw, i checked the positions at transfrom, and pasted the same positions (so where it needs to spawn) at the elements.
     
  9. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Are you sure that you didn't mix up the local and global position? That might be it. :confused:
     
  10. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I clicked global to local but I still got the same problem? Is that what you ment (uncheck global to local)?
     
  11. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    It would be nice if someone could help me with this.