Access script based on which one is active
I am trying to create an FPS with multiple weapons. Each weapon has it's own script. I also have a Weapons class that controls the more general properties and actions of all of the weapons. All of the scripts for each weapon will have the same methods and variables other than a few exceptions.
I can't use one variable for all of them since the types will be different for each one. The only way that I am seeing to be able to access the correct script for the method that I want is to search through all of them for the active script every single time that I want to use a method or variable from it.
How can I access these methods and variables for the weapon that I set as currently active?
Answer by SpaceManDan · Sep 27, 2015 at 07:02 AM
If you are interested in doing it right, it sounds like you are ready to learn about inheritance. I know this seems like a pain, but you are ready for it.
Thanks, this looks like exactly what I need, going to have to study it for a bit to make sure that I get it right. I figured out a way to do it on my own, but I know that it isn't the right way to do it. Have to run a bunch if statement every time I need to call any method on my weapon to find which one is active and then use Invoke to run the method. Super messy.
Thanks again, going to look into this and then refactor my code once I am comfortable with inheritance.
I can say this, it looks really confusing but it really isn't. If you get stuck, show me how and I'll do my best to get you moving again. Trust me, learning this is worth it. Inheritance is how you keep 'duplicate code' from being a thing in your project.
Think of it like this: Base class = parrent class Inherited class = child class
Could you make objects in unity without knowing how to child to a parent??? :) It's really important to understand that inheritance is childing a class to a parent class.
After watching that video from the link you provided I saw that it was exactly what I needed. I understand some very basics of inheritance, but need to look more into it. I am going to look more into the detail of it in the next few days to get to where I am comfortable with it. Thanks for pointing me in the right direction and I will hit you up if I get stuck. Thanks a lot for pointing me in the right direction.
Answer by vladrybak · Sep 27, 2015 at 06:32 AM
There are many solutions to achive what you want. It's a question of architecture of your project.
You can make an instances for your weapon classes.
public static MyWeaponScript instance;
void Awake(){
instance = this;
}
And depending on which weapon is active, call MyWeaponScript.instance
or more specifically,
$$anonymous$$yWeaponScrpit.instance.my$$anonymous$$ethod();
or
$$anonymous$$yWeaponScrpit.instance.myVariable = thisValue;
Your answer
Follow this Question
Related Questions
Why can't I access a variable from a different script? 0 Answers
Pass Variable Through C Script 0 Answers
How can I access variables from other scripts? c# 2 Answers
Copying variables in game? 0 Answers