- Home /
Adding component to component
Hi, i am new to component object models. Here is what i want to achieve.
I have an actor component for my game objects. The actor component as a private field of type behavior. I need to be able to extend the behavior class and add it to the actor. Like this.
Why not just add it as another component and access it through GetCommponent
?
The reason you can't just drag it on there is that the variable in Actor requires an instance of the script (not just a reference to the script). So if you add the PlayerBehaviour to the GameObject as Benproductions suggests you can get it through GetComponent. Any object with that script added could be dragged and dropped onto that variable in the inspector.
Can i make the Behavior field be a type and then instantiate from the type ?
Answer by MikeNewall · May 21, 2013 at 03:28 AM
Do you want to switch between behaviors at runtime? Declare your behavior field like you have already. When you want to add a different behavior simply destroy the old one and add the new:
Destroy(behavior);
behaviour = gameObject.AddComponent(typeof(Attack));
Yes i would need to be able to modify them at run time. I cannot instantiate Attack since its not a component but a scriptableObject.
Your answer
Follow this Question
Related Questions
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers
Destruction Help!!!!! 1 Answer
How to make a box (or any object) appear on collide? 1 Answer
Coding Gui Text To Perform After GameObject Destroy? 1 Answer
Player 1 and Player 2 move together 1 Answer