- Home /
C# Array with random.range help?
Hey guys I'm trying to get this random.range stuff to work where the scene is a car race, and you got to collide into a cube and one of two game objects appears. I want to make it so that both objects appear randomly when you collide with the cube. Like hit one cube it will be a yellow one hit the next one and it will be red, then hit the third one and it might be red again?
using UnityEngine; using System.Collections;
public class PowerUp : MonoBehaviour {
public GameObject[] powerupObject;
void OnTriggerEnter(Collider collision)
{
GameObject newPowerup;
newPowerup = Instantiate(powerupObject [0], this.transform.position, Quaternion.identity)as GameObject;
Vector3 temp = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
temp.y = 5;
temp.z = this.transform.position.z + 10;
newPowerup.transform.position = temp;
Destroy (this.gameObject);
}
}
If you have two items in your object array then you have two options, 0 and 1, in terms of the array index. So use random range to pick either 0 or 1, then use this value in your object instantiate.
can you help me write it out. I'm sort of new to c# scripting and still learning arrays at the moment?
Take a look at these parts of the documentation for a very good explanation as to how these work(If you still are not sure after reading these than have a good Google).
http://docs.unity3d.com/ScriptReference/Random.Range.html
http://docs.unity3d.com/ScriptReference/Array.html
It is not that hard(a very very small amount of code to add to the script you have found) and it is a good opportunity for you to learn, if I write it out for you then you will learn nothing and you will have to ask us to write it out for you again the next time, and the next until you do :)
okay I'm sort of stuck on this error.This is what I have changed so far. Can you just sort of give me a run through on this error? I've looked around for this error but everyones example and how they have written the script is far different.
Vector3 temp = Random.Range(newPowerup.transform.position);
and the error I get is PowerUp.cs(13,39): error CS1501: No overload for method Range' takes
1' arguments
Create an int variable and assign the random range(which will be 0 or 1) to this int variable.
Now use this int variable in your :
newPowerup = Instantiate(powerupObject [0], this.transform.position, Quaternion.identity)as GameObject;
line ins$$anonymous$$d of the 0.
newPowerup = Instantiate(powerupObject [USE_IT_HERE], this.transform.position, Quaternion.identity)as GameObject;
Answer by RadioactiveTechnologies · Nov 23, 2014 at 04:51 AM
Random.Range() takes 2 INTEGERS not FLOATS. Try that and see if it helps..
Still trying my best man. I've only been doing this for 5 weeks with only 3 hours of class time so I'm struggling here.
Answer by Jignesh G. · Nov 23, 2014 at 06:52 PM
Below code should work properly.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public GameObject prefab;
void Start() {
Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
Instantiate(prefab, position, Quaternion.identity) as GameObject;
}
}
and for your kind knowledge random.range accepts both integer and floats.
Your answer
Follow this Question
Related Questions
Linear interpolating (Lerping) a Gameobject between random positions of Other GameObjects 1 Answer
Random.insideUnitCircle seems to reproduce similar vectors 1 Answer
Vector3 Position returns 0 when applied to GameObject 1 Answer
How do I make create a rotateable object that follows a raycast hit point? 2 Answers
Spawn object using vector array anywhere but previous spawn location 1 Answer