- Home /
Spawning targets on 2 of 4 pre-defined locations using an array and empty game objects?
I am trying to spawn a target on pre-defined locations which will be the locations of empty game objects.
But i only want two targets spawning on the two of the four empty game objects i have.
From my understanding, the best way to do this would be to set up an array for the empty game objects and put the array though a loop that will randomly pick two numbers between 0 and 3, which will be linked to two empty game objects through the array. then spawn a target on that location, or have a target already there but just turn the renderer on for that target when the corresponding targets are picked.
At the minute all i have is one line of code that can turn a targets renderer on when true, and off when false. My initial intention was to write a new script (targetmanager) that will do the above, and then call into this other script and turn the false to true.
Ill have no trouble accessing this renderer script, I'm just having trouble getting my head round the 'randomly generating two numbers, linking these numbers with objects in an array and then making the whole thing work' part....
This may not be the correct way to do things and so any input provided will be taken on-board and no doubt prove useful in some way as i have no code for this yet.
Please try and be forgiving, im new-ish to programming, also i am using Java
Thank-you in advance
Answer by Fattie · Oct 01, 2013 at 09:11 AM
You are correct that you should have four "marker objects" (simply, empty game objects) which are the positions.
If I'm not mistaken you're just wondering the basic programming question of how to pick two of these randomly, is this correct?
I will actually go ahead and tell you the correct programming solution for this - so it's a world first for this list.
So you have the index numbers zero through three. You're with me so far right?
Notice there are four (count) such numbers.
var howManyNumbersDoIHave : int = 4;
No problem so far right?
Now your solution will be TWO INDEX numbers. Correct right?
var theFirstResultIndex : int
var theSecondResultIndex : int
The first one is easy.
theFirstResultIndex = Radom.Range( 0, howManyNumbersDoIHave );
{Aside -- Note that with Random.Range, the second argument IS THE TOTAL COUNT, it is not the "last number you want". So for example Random.Range(931, 1550) would START at 931, and there would be a possibility of 1550 numbers in total. So note that Random.Range(0,2) would give you either a "0" or a "1". So hopefully that's clear.}
Now here's how you do the second one. You travel in a loop around from where you are (the first one) but only as far as you don't pass the first one again.
var howFarToTravelAtMostInALoop : int;
howFarToTravelAtMostInALoop = howManyNumbersDoIHave - 1;
Makes sense right?
So you need a random number from ONE to and including howFarToTravelAtMostInALoop.
var howFarToTravel : int;
Let's pick a random number from 1 inclusive up to and including howFarToTravelAtMostInALoop
howFarToTravel = Random.Range( 1, howFarToTravelAtMostInALoop );
In your example this will give you either a 1, 2 or a 3
var whereILandedAfterTravellingInALoop : int;
Here's a big secret.
whereILandedAfterTravellingInALoop =
theFirstResultIndex + howFarToTravel;
but that could "go past the end" am I right? So you have to do this:
whereILandedAfterTravellingInALoop =
whereILandedAfterTravellingInALoop % howManyNumbersDoIHave;
theSecondResultIndex = whereILandedAfterTravellingInALoop;
Notice the magical "modulo" (arguably, "modulus") symbol. You can learn about this easily by doing a four-year computer science / pure math double major course, so go do that.
All you need to know is it will make you travel in a loop.
Et Voila, you have the two answers:
Debug.Log("the two safe and different numbers are "
+ theFirstResultIndex +" "+ theSecondResultIndex);
Any other method for doing this is wrong. Hopefully nobody ever has to explain this again in this universe, and i hope it helps!
Regarding how to turn on and off the array, it's this simple ..
"set up an array for the empty game objects..." yes, that's correct
var myFourItems : GameObject[];
in the editor, drag the four items there. Then just do this
myFourItems[theFirstResultIndex].SetActive(false);
myFourItems[theSecondResultIndex].SetActive(false);
(or, if you wanted, turn the renderer on/off .. whatever is relevant to you.)
It could be you further more have complicated functions on those objects, so your script might be called "MyScript.js" which is attached to each of the four objects and which has functions like "MyFunction()". To access those, just do this...
myFourItems[x].GetComponent<MyScript>.YourFunction();
Relatedly, I also direct you to this long answer which is a basic in stuff like this; some of the example code for "accessing stuff in Lists etc" may be relevant here. http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Thank you very much....
In reference to taking a course on this, I am currently doing a 3 yr games design course but have only just started scripting.
So as soon as I'm done in today's lectures I will give your answer a go
Thank you for making it simple, loving those variable names :)
Your answer
Follow this Question
Related Questions
Spawn object using vector array anywhere but previous spawn location 1 Answer
Spawning Objects Using An Array. 1 Answer
Spawn object from Random Vector3 in array 0 Answers
How do I Spawn a random gameObject from Array1, at a random position of gameObjects from Array2 ? 1 Answer
How to fix some of location have more than 1 object? 2 Answers