Hey guys,
I’m relatively new to scripting and quite often find myself hitting a wall that no amount of forum searching will cure. I hope someone could help me out with this instantiate script i’m cobbling together and erroring.
Essentially I want this-
I have 3 treasure items and 2 spawn points and want to randomly choose one of the treasure items to spawn out of a randomly chosen window and then repeat after a random amount of seconds.
(This will happen during a countdown timer for 60 seconds but thats not yet in the code i have below nor is the loop, but if someone has the time to help out about that, that would be great as well- though i may be pushing my luck!)
For the moment though, I just need help as to why my two Arrays are erroring and any obvious other errors that stick out.
If there is a more efficient way to do this, I’d also be keen to hear that too.
Any help would be great, thanks in advance.
Heres what I have- (I’m getting errors at line.character- 1.24, 2.9, 2.12, 2.13 straight away)-
var windowVec : Vector3[Vector3(1,2,3), Vector3(2,3,3)]; //choose two window translates
var gold[] : new array("chest", "shield", "jewels"); //array containing 3 different gold items
var minWait = 1.0;
var maxWait = 4.0;
function Start () {
var windowRand : int = Random.Range(0, windowVec.Length); //windowRand chooses a number between 1 and 2
var goldRand : int = Random.Range(0, gold.Length); //goldRand chooses a number between 1 and 3
var windowSpawn : Vector3 = windowVec[windowRand]; //windowSpawn chooses the chosen number of the windowVec array
var goldSpawn : String = gold[goldRand]; //goldSpawn chooses the chosen number of the gold array
Instantiate(goldSpawn,windowSpawn,Quaternion.Rotation); //instantiates chosen goldSpawn, at windowSpawn location
yield WaitForSeconds(Random.Range(minWait, maxWait)); //waits for randomly selected amount of time
}
Start() is only run once at startup. So it spawns an item, waits for a random amount of time, then it’s done.
Hey Clarkster, thanks for the reply but its not so much the looping i need help with. I’ll change function Start() to function spawnTreasure() and loop it eventually. For the moment I’m just wondering why i cant even get that first time to instantiate at Startup, like why is it erroring out over my first two arrays (lines 1 and 2)?
I should have been clearer in my post sorry.
would something like
for(i = 0; i < numberOfPositionToFill, i++)
{
var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0));
Instantiate(prefab, position, Quaternion.identity);
}
Hey renman3000, The two spawn positions are fixed positions like (0,16, 2.4) and (1, 16, 4.8) so they cant be a random range.
Hmm, sorry. I obviously didn’t read your post thoroughly.
Can you list the errors you are getting? I’m a C# guy so I am not so sure about the JS syntax.
Line 1 strikes me as odd.
How can you have two vectors in one vector?
Clarkster and Renman3000- The errors i’m assuming have to do with how I’m declaring the two vector3’s. In a nutshell my problem might be that I dont know how to store two transforms in an array to be chosen randomly between. Frankly i cant find how to store two vector3’s in the one array anywhere on this forum or the web. The errors are:
1.24 Assets/Scripts/SCRIPTspawn.js(1,24): UCE0001: ‘;’ expected. Insert a semicolon at the end.
2.9 Assets/Scripts/SCRIPTspawn.js(2,9): UCE0001: ‘;’ expected. Insert a semicolon at the end.
2.12 Assets/Scripts/SCRIPTspawn.js(2,12): BCE0043: Unexpected token: :.
2.13 Assets/Scripts/SCRIPTspawn.js(2,13): UCE0001: ‘;’ expected. Insert a semicolon at the end.
If someone could give me the proper way to put two vectors in an array that would be great, and I can figure how looping and such later.
Also the ‘gold’ array seems to get errors too but i cant figure out why, i thought it was the correct way to write it?
var vectorArrayType : Vector3[];
var vectorArray : array;
function Start()
{
vectorArray = new Array (vectorArrayType)
}
should do it.
Awesome thanks but where would i enter the values - for example (0, 16, 2.4) and (1, 16, 4.8)?
Thats the one thing i cant seem to find on any forum…
In the inspector.
I am new to arrays myself, you may be able to do this too but i am away from my computer so not sure…
var vectorArrayType : Vector3[“1,2,3”, “1,3,2”, “3,2,1”];
Great thanks, ill give it a shot when i get a chance. Thanks for your help guys.