- Home /
Object moves to new location in vector array
I have no problem instantiating my object randomly in one of numerous locations which are defined in my vector array:
Instantiate (myObject, myVectorArray[Random.Range(0,myVectorArray.Length)], transform.rotation);
But how can I move the object randomly to a new location in this array without destroying the object and re-instantiating it. Ultimately, when the user touches the object, I want it to move to a new location in the array... Thanks!
Answer by john-essy · Apr 12, 2012 at 11:10 PM
All i would do with this if i understand you correctly is
Psuedo code!!!
function OnTriggerEnter(hit : Collider)// or collider
{
if(hit.gameObject.name == "object name")
object.transform.position = myVectorArray[Random.Range(0,meVectorArray + 1)];
}
The reason the plus one is there is because random.range will not return the last item in the array as it is exclusive of the last
when using Random.Range to generate an Integer. For floats , the max is Inclusive
http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613070246im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i spawn in a prefab with a x,y,z location? 2 Answers
Spawning Objects Using An Array. 1 Answer
Collecting Object Array When Instantiated 0 Answers
Getting a 2d Sprite to move over time to an Array 1 Answer
Default object placement location. 0 Answers