- Home /
Need help with easy scripting
Hello, I need help with something! I appreciate all help I can get! Thanks in advance//Tim
I want my object to spawn at random X position within for example -10 and 10. How can I make that happen? I also dont want any exact numbers, I want it to spawn at random time between for example 30 seconds and 45 seconds. This is how my script look like right now (i already made my object moving):
function Update () { transform.Translate(0, -0.1, 0 * Time.deltaTime); }
If you can add the code I need to make my spawn thingy work I would be awesome! Also some explanation to what you did =)
Answer by Scribe · Nov 16, 2010 at 06:13 PM
You might want to check this page out it has some explanation about random class and might be useful for what you want to do: Random.Range
var YourObject : GameObject; //select the object you want to spawn
var RandPos = Random.Range(-10, 10); //the position
var RandInterval = Random.Range(30, 45); //the time interval
InvokeRepeating("SpawnObject", RandInterval, RandInterval);
//the function "SpawnObject" starts after 30-45 seconds and repeates every 30,45 seconds
function SpawnObject () {
Instantiate(YourObject, Vector3(RandPos, 0, 0), Quaternion.identity);
}
I think this would work but you might have to tweak it to fit your needs
Answer by Proclyon · Nov 16, 2010 at 06:15 PM
There's several questions actually
You will need "this" for random values:
[Random.value][2]
and this for "spawning" anything:
I won't write the code, like your last post said, you wanted to learn. Well you can't learn programming without doing it sorry, that'd be bad Q&A aswell for this site.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083307im_/https://answers.unity.com/themes/thub/images/avi.jpg)