|
Hi everybody, I'm trying to switch from a grid to a circle representation of a GameObject (prefab), but I cannot get the transition correctly, here is the code: it should instantiates a prefab in a grid and pressing "Fire1" key, should change to the circle, does it make sense? how can i do it? thanks in advance for anyhelp...
(comments are locked)
|
|
The 'Start' function only gets called once- in the first frame that the object exists. So, it only checks the fire button once- when it is first created. If you want a transition, you will need to do a few things. 1: Keep track of the currently existing objects. If you don't know what you've already created, you won't be able to delete them before creating the other pattern! Simplest way to do this is to use one of the features of transform parenting. In your instantiation code, do this: This will make all the new object children of the controller. Then, when you switch patterns, do this: This will automatically delete all the children of the controller's transform! Then, refactor it a bit. Put everything between about 'if(!isGrid)' and the end of the bit that spawns the grid into a new function: Then to activate it, do this: Remember to delete the previous pattern before instantiating the new one. hey, thanks a lot, I'm very newbie scripting and don't know if i understood you very well, so let me paste the code again to get the chance of feedback, ok? bytheway i got this error: expecting (, found 'ToggleGrid' thanks again,
May 10 '12 at 06:55 PM
sebascontra
You declared the function 'ToggleGrid' inside another function!!! You can't possibly expect that to work. You need to understand how functions work in c-like languages: in this case, anything inside the 'Start' function (the bits between the curly braces) will only run when the object gets created. You should remove that entirely, since it's only causing problems for you. You should add the bit for removing the existing grid into the beginning of the 'ToggleGrid' function- don't put it in Start, either.
May 11 '12 at 03:41 AM
syclamoth
(comments are locked)
|
