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 /
This question was closed Jun 26, 2017 at 06:46 PM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ItsBoats · Jun 25, 2017 at 12:40 AM · inheritanceoverride

Override Function Scope

I'm having some trouble accessing an instance variable in an overridden function after setting it in the start function.

The Debug.Log just returns that the level is still 3 inside of Activate_Skill() but it returns 5 inside of the update function. How do I get the same value inside of Activate_Skill()?

I have tried changing the variable to public, and declaring it in the base class, initializing it to 5 in the base class start function etc,

 public class SubClass : BaseClass
 {
     private int level = 3;
 
     private void Start()
     {
         level = 5;
     }
 
     private void Update()
     {
         Debug.Log("level: " + level);
     }
 
     public override void Activate_Skill()
     {
         Debug.Log("Level: " + level);
     }
 }
 
 public abstract class BaseClass : MonoBehavior
 {
     public abstract void Activate_Skill();
 }
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

  • Sort: 
avatar image
1
Best Answer

Answer by ItsBoats · Jun 26, 2017 at 08:11 AM

The problem turned out to be that I was basically calling Activate_Skill() in a prefab object instead of the instanced object. So while I was seeing that there was an instance of the object from the debug message in the update function, I was calling the Activate_Skill() function from the prefab object which obviously never executed its Start() function. The above code was pretty irrelevant to the issue, but I figured it was that I was doing something horribly wrong with the abstract/override keywords

Comment
Add comment · 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
1

Answer by Bunny83 · Jun 25, 2017 at 01:21 AM

Well, you clearly call your Activate Skill method before your Start method is called. Start is called quite late. It's called right before Update.

Instead of Start() use Awake()

Comment
Add comment · Show 4 · 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 ItsBoats · Jun 25, 2017 at 04:22 AM 0
Share

I'm calling it in the update function of my player controller script after a keypress, so it is well after Start() and Awake()

avatar image Kishotta ItsBoats · Jun 25, 2017 at 04:31 AM -1
Share

Have you tried making BaseClass abstract?

avatar image ItsBoats Kishotta · Jun 25, 2017 at 05:49 AM 0
Share

Thank you for pointing that out, it should have said that in the topic question already. Updated code above to reflect that.

Show more comments

Follow this Question

Answers Answers and Comments

68 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

Related Questions

Overriding an initial value in a subclass? 3 Answers

Question about Overriding 1 Answer

Overriding an inherited Enum? 1 Answer

Help with override function in inheritance,HELP with inheritance overide functions 0 Answers

An OS design issue: File types associated with their appropriate programs 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