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 F-N · Mar 23, 2016 at 02:52 PM · c#classes

Inherit from specific classes

Hi there everyone, here is what I am trying to do:

I would like to creat a class that inherits all variables from a script that is active in the scene.

for example I have a class called Menu:

 public class Menu : MonoBehaviour{
     public int i;
     void Update(){
         if(Input.GetKeyUp(KeyCode.Space)){
             i = 5;
         }
     }
 }
 

And I have a MenuOptions class:

 public class MenuOptions: Menu {
     void Update(){
               print(i);
         }
 }

Now I want the MenuOptions to print 5 but I have no idea how I could achieve this... Any help is appreciated!

Comment
Add comment · Show 6
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 Owen-Reynolds · Mar 23, 2016 at 03:15 PM 0
Share

Thanks not what inheritance does. It seems like you just want one script to know about another. There are lots of old answered Qs here that should help explain that. Titles like "Find another Script" or "One script use variables from another."

avatar image Fredex8 · Mar 23, 2016 at 03:21 PM 0
Share

Well that will work if you make it static: public static int i; but I don't think it is the best solution.

For something like this you probably want to set it with GetComponent or Send$$anonymous$$essage when the value needs to change.

avatar image F-N Fredex8 · Mar 23, 2016 at 04:14 PM 0
Share

Thanx for the replies, I wonder if this way of inheriting will be any good but I believe it's pretty useless since I found out I didn't want to use any variables from the $$anonymous$$enu class and since it might be more handy to use GetComponent.

avatar image NoseKills Fredex8 · Mar 23, 2016 at 04:38 PM 1
Share

Why static? $$anonymous$$enuOptions is a subclass of $$anonymous$$enu.

Not really sure what the OP wants, but all that seems to be missing are the "virtual" and "override" keywords and a call to base.Update(), and an instance of $$anonymous$$enuOptions would print 5 when space is pressed.

avatar image Fredex8 NoseKills · Mar 23, 2016 at 04:49 PM 0
Share

I don't know to be honest. I just know that it works if you change it to static. As I said though... there are better solutions.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

112 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 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 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 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

Calling Function from another script not working 1 Answer

Disable a group of scripts based on base class. 0 Answers

Can't figure this out! C# access another class 2 Answers

Multiple PowerUps (One ItemBox) 0 Answers

Class value as list of the same class 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