• 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
Question by DTE462 · Jan 14, 2017 at 12:50 PM · setactiveinstantiationreference-other-object

Set Hierarchy Object Active from Instantiated Object

I have a Canvas that is Inactive in the Hierarchy. When an instantiated object (Object A) collides with a specific object in the $$anonymous$$erarchy (Object B) there is a script on Object B that will set the Canvas to active and set text to somet$$anonymous$$ng. That works fine.

But I want to do the same t$$anonymous$$ng with two objects that are instantiated (Objects A and C collide). I can't get Object A to reference the Canvas in the Hierarchy. I turned the Canvas into a prefab and linked the two objects that way, and it won't give me any errors, but when I call SetActive(true) from the instantiated Object A not$$anonymous$$ng happens.

I've also tried tagging the Canvas and FindGameObjectWithTag during Start() but that throws an error.

 // LevelExit.cs

 // T$$anonymous$$s Works
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Hero")
     {
         notice.gameObject.SetActive(true);
         noticeText.text = "Press 'A' to go to next Level!";
     }
 }

 // HeroScript.cs 

 //T$$anonymous$$s Doesn't
 
 public Canvas notice; // Prefab of canvas (already in $$anonymous$$erarchy)
 
 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "enemy")
         {
             notice.gameObject.SetActive(true);
             notice.GetComponentInC$$anonymous$$ldren<Text>().text = "You $$anonymous$$t an enemy";
         }
     }



Comment

People who like this

0 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 jwulf · Jan 14, 2017 at 12:56 PM 0
Share

You seem to be mixing up instances and prefabs. You have apparently created a prefab of your canvas and then linked that newly created prefab to the (prefabs of the) instantiated objects. But of course the prefab is not the same as the canvas in the hierarchy. It is just the prefab for the specific object in your scene.

"I've also tried tagging the Canvas and FindGameObjectWithTag during Start() but that throws an error." - Well, what error? Did you read it? If so, did you understand it? If not, please provide the error message, maybe then we can help.

avatar image DTE462 jwulf · Jan 14, 2017 at 05:57 PM 0
Share

That makes sense. Error is just the null reference error. The instantiated script isn't finding the tagged inactive Canvas. I assume it's because it's not active. I guess one work around is to put the script on the Canvas and make it active. Then make each individual element in the Canvas active and inactive as needed.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with shooting multiple bullet at the same time with an Object Pooler 1 Answer

Turret not spawning fireball via instantiation in Unity 2d? 0 Answers

Instantiating multiple interactive prefabs 0 Answers

Object reference not set to an instance of an object. Dynamic target change. 1 Answer

Class selection screen not working 0 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