• 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 Roixo · Jan 16, 2018 at 05:35 PM · reference

How get a Component By Reference

I have several instances of one prefab which have this PropertiesPrefab script attached:

 public class PropertiesPrefab : MonoBehaviour {
     
     public float health;
     
 }

In other script, i want to access to the health of the PropertiesPrefab script of the prefab:

 public class AnotherClass : MonoBehaviour 
 {
     PropertiesPrefab propsPrefab;
     
     void OnTriggerEnter(Collider other) 
     {
         propsPrefab = other.GetComponent<PropertiesPrefab>();
     }
     
     
     void OnTriggerStay (Collider other)
     {
         // Wrong value because is not referenced to the component
         Debug.Log(propsPrefab.health);
         
         // Right value
         Debug.Log(other.GetComponent<PropertiesPrefab>(). health);
     }
 }

The problem is that the debug.log sentences shows different values if the health has been changed. So the question is:

¿How can I get a component by reference, so when the health is changed, it also changes in the propsPrefab variable?

Comment
Add comment · Show 6
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 dev-waqas · Jan 16, 2018 at 05:38 PM 0
Share

You want the health to be changed for all prefab instances?

avatar image Roixo dev-waqas · Jan 16, 2018 at 05:42 PM 0
Share

No. Each instance of the prefab has its own health, that can change during the OnTriggerStay.

I need to get the health by reference in the OnTriggerStay for the other collider.

avatar image myzzie Roixo · Jan 16, 2018 at 05:51 PM 0
Share

And this is with one gameobject staying in the ontriggerstay? I can see issues with multiple objects and only 1 reference.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 16, 2018 at 06:01 PM

Your question is confusing. You do store a reference to the "PropertiesPrefabClass" script that has last entered the trigger. Are you sure there is only one object that is inside the trigger? You may want to use a proper Debug.Log statement like this:

 Debug.Log("Health(propsPrefab): " + propsPrefab.health + " Obj: " + propsPrefab.gameObject.name, propsPrefab.gameObject);

and

 Debug.Log("Health(other): " + other.GetComponent<PropertiesPrefabClass>(). health+ " Obj: " + other.gameObject.name, other.gameObject);
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 Bunny83 · Jan 16, 2018 at 06:06 PM 0
Share

The second argument of Debug.Log allows you to specify a "context" object. When you click once on the log entry in the console Unity will highlight the context object in the project / hierarchy panel.

avatar image Roixo · Jan 16, 2018 at 06:11 PM 0
Share

Imagine I have 2 spheres colliding all the time. And another external script can modify the health of the Sphere 2 any time. I want to check inside the OnTriggerStay of the Sphere 1 if the health of the Sphere 2 has been changed, without doing other.GetComponent<PropertiesPrefab>().health; all the time, and just checking propsPrefab.health

avatar image myzzie Roixo · Jan 16, 2018 at 06:30 PM 0
Share

I just set it up, and everything is working fine. Script on plane deal damage to sphere 2. Sphere 1 holds the trigger stuff. and sphere 2 holds the health script.

 public TestHealth testHealth;

 void OnTriggerEnter(Collider col)
 {
     testHealth = col.GetComponent<TestHealth> ();
 }

 void OnTriggerStay(Collider col)
 {
     
     Debug.Log (testHealth.health);
 }
avatar image Roixo myzzie · Jan 16, 2018 at 06:37 PM 0
Share

For example, If the health is changed 3 seconds after the 2 spheres start colliding, in your log statement, after 3 seconds, you get the old healthvalue ins$$anonymous$$d of the new one .

avatar image
0

Answer by smflt_nhn · Feb 08, 2021 at 03:10 AM

Assets that can be used to find references have been released.

You can find and fix missing components or references.

Image:

Visual display of asset relationships. alt text

Image:

Find detailed references to assets. It's a texture, but it's also script. alt text It's FREE! Please manage your assets conveniently and solve the problem of your project.

Detailed explanation is in the link below.

https://forum.unity.com/threads/free-released-assetmanagement-integrated-asset-management-system.1051568/

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

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

78 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

Related Questions

Reference creator of gameObject 1 Answer

Storing References of Custom Classes 0 Answers

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Object Reference not set to an Instance 2 Answers

Object reference not set to an instance of an object. 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