- Home /
Efficient way to assign a value from another value
Hey guys, (i'm not new to UnityAnswers, just have a new username due to the QATO port)
OK, i have a script that places objects on a certain X co-ord
objectCopy.transform.position.x = array[whichNumber];
the whichNumber variable obviously tells the objectCopy which array item to choose ( this is out of integers -7,-5,-3,-1 etc to 7 - so 8 positions. )
I have another line in the same method which does this;
if ( whichNumber == -7 ) { whichType = 0; } else if ( whichNumber == -5) ...etc
and that goes on for the 8 position ( -7, -5, -3... etc.)
whichType is for the Instantiate of objectCopy which is an array of gameObjects...
so that if the object was placed at -7, whichType would be 0 meaning it'll place object 0 of the array at -7... object 1 of the array at -5 etc...
MY QUESTION - (finally, lol!)
Is my if... else if... else if... else if... the best way to assign whichType from whichNumber? i feel there should be a more efficient way, unless my noobishness is leading me astray and this is perfectly acceptable?
Thanks for the help guys and girls, another stepping stone for me!
Matty.
Answer by Eric5h5 · Jun 08, 2011 at 06:01 AM
Since you have a regular sequence, just use a bit of math.
whichType = (whichNumber + 7) / 2;
That is magic. Thank you Eric, math has never been my strong point, $$anonymous$$cher in High School would walk past me when checking homework, knowing I wouldn't of done it.
Not that I wouldn't of tried, I just struggled to understand... Putting it into practice though has been great, since I started program$$anonymous$$g games.
Anyway! thank you again, see you around.