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 /
avatar image
0
Question by Gamershaze · Apr 17, 2016 at 10:26 AM · c#runtimevariablesinheritanceinstance

C# Inheritance - Reference the INSTANCE of a variable, is it possible?

I've been digging around and have found many similar but not exactly matching results to this question, all of which have made this infinitely more confusing. Below is example mock-up code of what we're trying to do here. Please read the bottom paragraph for an explanation. Thank you!

Base.cs

 using UnityEngine;
 using System.Collections;
 
 public class Base : MonoBehaviour {
 
     public GameObject Chase;

     void Update () {
     Chase = GameObject.Find("Chase"); // Never GO.Find on Update, this is just a mock-up.
     }
 }

Race.cs

 using UnityEngine;
 using System.Collections;
 
 public class Race : Base {
 
     void Update () {
         Debug.Log(Chase);
     }
 
 }
 

With that in-mind, is it possible to get the instance of a variable that we're inheriting from? The idea here is having a 'base' class that houses a couple variables that will be used across all scripts within the project. These variables are assigned during run-time, so we need to grab the Instance of them. However at this point, I am no longer sure if this is something inheritance will support. Statics don't seem like the way to go, and namespaces/interfaces are just as useless in this case scenario.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Apr 17, 2016 at 11:16 AM

Singleton solution could look like this for example:

 //add this class to your Chase gameObject and make sure it's in the Scene somewhere:
 public class ChaseClass : MonoBehaviour {
     
     private static ChaseClass _singleton;
     public static ChaseClass singleton {
         get {
             return _singleton;
         }
     }
 
     public static GameObject go {
         get {
             return _singleton.gameObject;
         }
     }
 
     void Awake () {
         //address singleton
         if( _singleton==null ) {
             _singleton = this;
         }
         else if( _singleton!=this ) {
             Destroy( this.gameObject );
         }
     }
 
 }
 
 //this is how you will access this Chase gameobject from any class:
 public class AnyClass : MonoBehaviour {
 
     void Update () {
         Debug.Log( ChaseClass.go );
     }
 
 }



Comment
Add comment · Show 2 · 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 andrew-lukasik · Apr 17, 2016 at 11:24 AM 0
Share

Not as good but still working solution based on inheritance could look like this:

 public class Base : $$anonymous$$onoBehaviour {
     
     protected static GameObject Chase;
 
     virtual protected void Awake () {
         if( Chase==null ) {
             Chase = GameObject.Find( "Chase" );
         }
     }
 
 }
 
 
 public class Race : Base {
 
     override protected void Awake () {
         base.Awake();//calls Awake() as defined in parent class:
         Debug.Log( Chase );
     }
 
     void Update () {
         Debug.Log( Chase );
     }
 
 }
avatar image Gamershaze andrew-lukasik · Apr 18, 2016 at 05:08 AM 0
Share

Thanks a lot for the help! While I'm sure the code within the first post will help someone out there, it doesn't quite help me in this scenario. The variable(s) we're trying to use in the 'base' class could be anything, from a GameObject to an int/bool. Any variables that will be used across all scripts within the project, which is exactly why we need to grab the instance themselves. With that said, I'll definitely give your seconds script a try when I get the chance!

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

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cram variables inside class, use inheritance, or something else? 1 Answer

inheritance - instance variable problem? 4 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