Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 LpScorp10n · Jul 17, 2019 at 07:29 PM · canvasgetcomponenthierarchysetactiveabstract

Getting component from derived classes

Hello there !

I'm still new at the unity coding but I do know how to workaround with c# but I'm having doubts in what to do with my aproach. So the idea is to have the abstract class Habilities and three derived classes: HabilitiesAttack, HabilitiesDefense, HabilitiesRogue. Each derived class will be in a canvas and each canvas will represent an individual tech tree. The access to this three canvas will be by buttons through OnClick using a dedicated function that sets active the screen to be opened and deactivates the current one. This is all done in the canvas habilities menu which has this method:

     void Awake() => Init();
 
     private void Init()
     {
         previousScreen = GameObject.Find(previousScreenName);
 
         /*attack = new HabilitiesAttack(gameObject.name, dbManager.GetAttackPoints());
         defense = new HabilitiesDefense(gameObject.name, dbManager.GetDefensePoints());
         rogue = new HabilitiesRogue(gameObject.name, dbManager.GetRoguePoints());*/

         //attack = gameObject.AddComponent<HabilitiesAttack>();
         attack = GameObject.Find("Canvas_HabilitiesAttack").AddComponent<HabilitiesAttack>();
         attack.FillSettingsData(gameObject.name, 100);

         defense = gameObject.AddComponent<HabilitiesDefense>();
         defense.FillSettingsData(gameObject.name, 100);

         rogue = gameObject.AddComponent<HabilitiesRogue>();
         rogue.FillSettingsData(gameObject.name, 100);

         Debug.Log(attack.gameObject.name);
     }

The problem is when I try to debug log the name or even use the button to change the screens it will give me: NullReferenceException: Object reference not set to an instance of an object HabilitiesMenu.Init () (at Assets/Scripts/HabilitiesMenu.cs:25) ----> Canvas_HabilitiesAttack HabilitiesMenu.Awake () (at Assets/Scripts/HabilitiesMenu.cs:15)

But if I have the Canvas_HabilitiesAttack active right from the start it will work. Isnt the Awake beingcalled because of the canvas not being activated? What do you recommend that I would do? Also I m putting the scripts in the canvas itself and using the canvas itself to the object on the OnClick buttons as it show on the following pictures.

alt text

alt text

damn1.png (48.7 kB)
damn2.png (52.9 kB)
Comment
Add comment · Show 2
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 Vega4Life · Jul 18, 2019 at 04:35 PM 0
Share

GameObject.Find won't return any disabled gameObjects. I would just stay away from using it ever - as it has perf issues.

Can you not just reference the GameObject straight away on the script thats trying to find it?

avatar image LpScorp10n Vega4Life · Jul 18, 2019 at 11:24 PM 0
Share

I m not sure what to do. I had an approach but I think that is necessary to make the abstract class and derived classes being apart from all the canvas. I think I should have a controller for each canvas that should use an instance of the respective derived class. What do you think? BTW how should I reference the gameobject?

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

187 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

UI will not reenalble after it was disabled 0 Answers

How to open and close a "message box"? 0 Answers

Enabling a disabled Canvas containing a ScrollRect makes the content jump to the bottom 0 Answers

How can I organize menus as a tree hierarchy? 0 Answers

Clicking Cube And Canvas Appears and disappears 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