Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MorganLee909 · Sep 26, 2015 at 05:34 AM · getcomponentvariables

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Inheritance Tutorial

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MorganLee909 · Sep 27, 2015 at 07:24 AM 0
Share

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.

avatar image SpaceManDan MorganLee909 · Sep 27, 2015 at 07:28 AM 0
Share

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.

avatar image SpaceManDan MorganLee909 · Sep 27, 2015 at 07:31 AM 0
Share

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.

avatar image MorganLee909 · Sep 28, 2015 at 04:00 AM 0
Share

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.

avatar image SpaceManDan MorganLee909 · Sep 28, 2015 at 05:13 AM 0
Share

anytime. Good luck!

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpaceManDan · Sep 27, 2015 at 07:05 AM 0
Share

or more specifically,

$$anonymous$$yWeaponScrpit.instance.my$$anonymous$$ethod();

or

$$anonymous$$yWeaponScrpit.instance.myVariable = thisValue;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't I access a variable from a different script? 0 Answers

{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons. 1 Answer

Pass Variable Through C Script 0 Answers

How can I access variables from other scripts? c# 2 Answers

Copying variables in game? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges