- Home /
Changing Enemy Prefab Variable (MoveSpeed) at Runtime
Hey Guys,
I just started working with Unity3D after a long period of working with the Unreal Development Kit. I believe that i know what i'm doing but i'm having issues with the following:
How can i increase the MoveSpeed variable of my prefab enemy when the player levels up (during runtime)?
I'm instantiating the enemies using a spawnpoint object. The PlayerLevel class is in the player object. Spawnpoint is an object. Enemy is a prefab.
I tried alot of things allready and i made a really long post with screenshots but that post got deleted because i had to register to unityAnswers first :/
If more info is needed i will provide more.
Thanks in advance guys
Are you trying to change the instance of the prefab or the prefab itself?
I was trying to change a variable of the instance of the prefab.
I finally got it to work. $$anonymous$$y solution:
$$anonymous$$ake a variable for the movement speed in the PlayerLevel class.
$$anonymous$$ake the AI look at that variable.
//I found this the only way to change the movespeed of the enemies during runtime.
PlayerLevel playerLevel = (PlayerLevel)player.GetComponent("PlayerLevel");
_moveSpeed = playerLevel.enemy$$anonymous$$oveSpeed;
---Is this way correct or is there a much better solution?
Thank you for replying
Answer by yezzer · Apr 14, 2012 at 06:03 PM
So many ways to do this!
Another way would be to tag all the enemies with, say an 'enemy' tag. Then do a FindGameObjectsWithTag, then in your enemy script, have a method called "ChangeSpeed". Then simply do a SendMessage("ChangeSpeed",whateverspeed); to all those gameobjects.
OR
Parent all the enemies to an empty game object ("EnemyContainer"?), and call BroadcastMessage on that gameObject when the player levels up.