- Home /
idea for a script of a gameobject with different types/roles
Hey i have about 6 gameobjects that serve different roles in my game and i want to have the same script attached to them since they will act the same but have certain differences. I was thinking of having different classes for each role in the script. But since Unity will only access the class that bears the same name as the script i have reconsidered it.
How should i do it? I was looking up enums but i am unsure of how they work. Do you guys have any idea on how i should do? I wanna avoid having to make different scripts for each different gameobject, considering they will play like almost the same. The differences is in the way they move, interact with other objects and their position in the gameworld.
Answer by Gobaz · May 28, 2016 at 09:15 AM
If you dont want to use inheritance or abstraction (would require each role having its own class inheriting from a general parent.
Then i would probably use an enum.
public enum RoleType
{
Knight,
Ranger,
Mage
}
public class Character : Monobehaviour
{
public RoleType; // Set this value in the inspector for each gameobject
private void SomeAction()
{
switch(RoleType)
{
case Knight :
DoKnightActions();
break;
case Ranger :
DoRangerActions();
break;
case Mage :
DoMageActions();
break;
}
}
}
As an aside and to address the technical aspect of enums: they're really just a labeled list of integers. $$anonymous$$ind of like an array of integers with a unique name for each, they can be cast back and forth to an int and used for an index or other things like that. It's also the reason that the switch statement works with them, because they're really just integers behind the scenes.
This is a good solution too but this involves a switch check in every update or fixedupdate (if it's not a single action) which is unnecessary. And in update or fixedupdate you want to avoid those if you can. A few won't cause a problem but it's not a good practice because it can affect performance if you do this a lot.
I agree that inheritance is the better solution overall, but if the OP doesn't want to add any classes for whatever reason then I think this is the optimal choice. It also doesn't necessarily involve the switch check in every update, only when the method is called.
There's a third option: Actions. I'll explain if anyone needs it. $$anonymous$$ethods can be saved as Action. It requires the System namespace. you create an action like this:
Action act;
void Start() {
act = $$anonymous$$y$$anonymous$$ethod;
//actually there's more to it but this is the easiest way.
}
void Update() {
act.Invoke(); //calls the method
}
public void $$anonymous$$y$$anonymous$$ethod() {
//whatever
}
For the record i still think inheritence is the best because classes can hold their own variables and the $$anonymous$$onoBehaviour class doesn't need to hold them if they are unnecessary.
Hey thanks! I was unsure on enums because i forgot how to access them through the inspector. I wont be needing to add any more roles so this will look good for me! :)
EDIT: Note for everyone else, you will need to also name the declared enum variable inside the class or else it will not run, so remember that! ^^
Answer by JedBeryll · May 28, 2016 at 09:28 AM
One good way is to create certain behaviors with inherited classes. For example: You have a monobehaviour script called VehicleController and in that a class called AIBehaviour, and have sub classes called CarMovement and MotorcycleMovement. They have virtual and override functions that you can call in udate or fixedupdate or however you want. It would look like this:
More info on inheritance: https://msdn.microsoft.com/en-us/library/ms173149.aspx
Thanks, this looks useful on how to have several classes in one script, i didnt know it could be done in Unity :) ill bookmark it but i found a simpler solution with enums now and i think its more what i wanted. Thanks anyways!