Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ScarletRain · Dec 24, 2013 at 05:46 PM · variablesstaticinheritancemonobehaviouraccessing

Access variables,method from a Mono Behaviour without static use.

Hello i am currently trying to work on a univercity project which requires us to make a 3D game and i've been trying to write my own code so i can train more my coding and better understanding of how c# works. Currently i have found some solutions to my problems but they seem quite "heavy" on my Desktop.

So i would like to have some pointers on how to implement or fix my code further to do the following.

My problem is that all my code works if i make the variables i want as static but that would pose problems on the code i would write afterwards. For example.

We have this classes:

 Char_Select : MonoBehaviour // this class requires mono behaviour because it has public gameobjects that need to be set in the inspector.
 
 Base_Character //which doesn't require to inherit Monobehaviour since it only initialization of variables and some functions.
 
 WizardSkills : MonoBehaviour // same as Char_Select

so far i was able to access methods and variables between those classes if i made static the variables and methods that needed to be accessed.Although this seems quite "wrong" and power consuming so i tried to do the following example.

 Char_Select instance = new Char_Select();

then i tried accesing the variables in the Base_Character class but as it seems because Char_Select inherits from mono behaviour i would have to use the GetComponent but as far as i know if i do that the whole class will get added to my character for example a Wizard.

 public class Char_Select : MonoBehaviour
 {
     public GameObject char1_glow;
     public GameObject char2_glow;
     public GameObject char3_glow;
     public Transform ranger;
     public Transform wizard;
     public Transform warrior;
     public bool isClicked = false;
     public int ID = 0;

Lets say this is our variables for Char_Select class

 public class Base_Character 
 {
     public  int Strength; 
     public int Agility; 
     public int Vitality;
     public int Intelligence; 
     public int Wisdom;
     private int level = 1;
     public bool up = false;
     private int xp;
     private int xp_to_level;
     public  bool incombat = false;

And this is the Base_Character class

How would i access without static and without needing to add the Base_Character and the Char_Select class to the gameobject (for example the Wizard) from the following class?

 public class WizardSkills : MonoBehaviour {
 
     public GameObject force;
     private bool shield = false;
     private int reduction; 
     private GameObject target;
 
 }
Comment
Add comment · Show 3
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 Benproductions1 · Dec 25, 2013 at 09:38 AM 0
Share

If you want to access a non static variable you need an instance of the Class. You can either assign this instance in the inspector or after instantiation, or in the case of components on the same object use GetComponent at some point. I usually have some kind of scene manager script that has all the references I need.

avatar image ScarletRain · Dec 26, 2013 at 05:19 PM 0
Share

so if i understand correctly i have to do something like a script that will [require[components]] of all my other scripts and create instances of my other code. i was thinking of making the functions and classes i want to connect virtual and then override them.Would that work? but i think i understand what you told me. Thanks a lot i will try to make it work. i hope if i have some other question i could get some more guidelines :)

avatar image Benproductions1 · Dec 27, 2013 at 12:53 AM 0
Share

$$anonymous$$aking the functions virtual wouldn't help at all. You're trying to access instance variables, not inherit behaviour. But you are correct in your earlier statement. That is how OO works :)

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

19 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

Related Questions

How can I edit my static variable in the editor? 1 Answer

Static Variables 1 Answer

How to watch variables from another script efficiently? 1 Answer

Previously functional script now mysteriously does not invoke Start or Update methods 2 Answers

Why child ScriptableObjects class instance cannot be placed in their parent ScriptableObject field in the inspector? 1 Answer


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