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 Capricornum · Jul 16, 2019 at 07:38 AM · inheritanceaccesspropertyscope

Access local method variable in derived class

Dear Community,

I have a class called BuildOption. In it I have a method called Build().

 protected virtual void Build(Hex targetHex)
     {
         Actor instance = Instantiate(actorPrefab);
         instance.GetComponentInChildren<MeshRenderer>().material = SelectedMaterial;
         instance.MyHex = targetHex;
     }

This method instantiates a prefab called actorPrefab, changes the material of the instance and changes some Property called MyHex.

I have a child class called MonsterBuild which inherits from BuildOption. In the child class I wish to override the method Build and add some functionality. Specifically I would like to change another property of 'instance'. However 'instance' is a local variable to the parent Build() method.

 protected override void Build(Hex targetHex)
 {
     base.Build(targetHex);
     instance.ID = Random.Range(1, 10); //this doesn't work because instance doesn't exist in this methods scope
 }

Is there a possibility to access the variable 'instance' in the child class? Or is there another way for me to change a property on 'instance' in the child method?

Thank you.

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
5
Best Answer

Answer by Hellium · Jul 16, 2019 at 07:54 AM

I see several possibilities.


 1. Declare the instance variable protected in your class so that your base class and derived class can access it.


 2. Make the Build function return instance. If the callers does not use the returned instance, it's not a problem

  // Base class
  protected virtual Actor Build(Hex targetHex)
  {
      Actor instance = Instantiate(actorPrefab);
      instance.GetComponentInChildren<MeshRenderer>().material = SelectedMaterial;
      instance.MyHex = targetHex;
      return instance;
  }

  // Derived class
  protected override Actor Build(Hex targetHex)
  {
      Actor instance = base.Build(targetHex);
      instance.ID = Random.Range(1, 10);
      return instance;
   }


 3. Use an additional virtual method to do the job

  // Base class
  protected void Build(Hex targetHex) // Notice we have removed the virtual keyword here
  {
      Actor instance = Instantiate(actorPrefab);
      instance.GetComponentInChildren<MeshRenderer>().material = SelectedMaterial;
      instance.MyHex = targetHex;
      SetupActor( instance );
  }

  protected virtual void SetupActor( Actor instance )
  {
      // Do nothing, overriden in subclasses
  }

  // Derived class
  protected override void SetupActor(Actor instance)
  {
      instance.ID = Random.Range(1, 10);
   }
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 Capricornum · Jul 16, 2019 at 08:11 AM 0
Share

This is really helpful. Thank you very much! I don't suppose there is an advantage of one over the other? The third one makes the most sense in my head. Just in terms of clarity.

Best wishes...

avatar image
0

Answer by hameed-ullah-jan · Jul 16, 2019 at 07:43 AM

what if you declare the instance variable out side the build method in the parent class and make it public so all your child classes and methods will access it.

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 Capricornum · Jul 16, 2019 at 07:49 AM 0
Share

I suppose that would work. I thought about it myself. But I feel that's an ugly solution because its prone to errors: What if I attempt to access the public instance variable, but it hasn't been set in the parent method? Or it's still something from time past? I was hoping there is another solution.

avatar image hameed-ullah-jan Capricornum · Jul 16, 2019 at 07:52 AM 0
Share

yes that is also possible, but for such scenarios i use abstract classes because, abstract classes cannot be instantiated you can only override its methods, so have a look over the abstract classes. hope that would help you

avatar image Capricornum hameed-ullah-jan · Jul 16, 2019 at 07:57 AM 0
Share

I'm afraid, I don't understand. You are suggesting I use an abstract class for 'Actor'? Or for the BuildOption? How would that help with the scope problem?

Show more comments

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

113 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

Related Questions

Get A Variable from another Script in Unity iPhone 2 Answers

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

Google Drive Plugin - full access from android device 1 Answer

Material doesn't have a color property '_Color' 4 Answers

"You are trying to create a MonoBehaviour using the 'new' keyword. 3 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