''Random spawn'' problem

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:

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.

  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.

What is an index out of bound exception?

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?

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.

An array is a set of variables that the computer stores as one solid block in memory. Each # is an integer for example:

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:

fiveIntegers[0]; // gives you the first element (int0 above)
fiveIntegers[1]; // gives you the second element (int1 above)
...
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.

Random.Range(min, max) includes min, but excludes max.
Random.Range(0, arrayOfSizeFive.Length) will return the numbers 0, 1, 2, 3 or 4.

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.

Are you sure that you didn’t mix up the local and global position? That might be it. :face_with_spiral_eyes:

I clicked global to local but I still got the same problem? Is that what you ment (uncheck global to local)?

It would be nice if someone could help me with this.