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 juliancruz87 · Jun 10, 2011 at 11:42 AM · gameobject

how I can get a property of a GameObject?

I put a script component, with a proprietary life, how I can obtain and modify the initial value from another object that collided with it?

Thank you very much.

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

Answer by ckfinite · Jun 10, 2011 at 01:16 PM

What you could do this in the object that hits the other object:

 function OnCollisionEnter(coll : Collider) {
     if (coll.gameObject.GetComponent(NameOfOtherScript) != null)
         coll.gameObject.GetComponent(NameOfOtherScript).YourProperty;
 }
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
avatar image juliancruz87 · Jun 11, 2011 at 02:26 AM 0
Share

It does not work, I am trying to access the property and I always get an error that says life is not property in the class GameObject. I created my property so, public int life = 100, and the script is called PlayerSettings, but does not work, so I'm also trying to create public static int PLAYERLIFE = 100, but neither worked.

avatar image ckfinite · Jun 11, 2011 at 02:50 AM 0
Share

Please post your exact code. The only things I can think of are related to syntactical problems.

avatar image
1

Answer by Joshua · Jun 10, 2011 at 01:14 PM

I'm not sure I understand your question, but I think you're meaning to ask how do I, from a script attached to gameObject A modify a variable in a script on component B when B collides with A?

If B has a rigidbody, when upon collision the OnCollisionEnter function will be called.

 function OnCollisionEnter( enter : Collider ){
     if( enter.gameObject == (gameObject_B)//you could also compare tags, or some other property to identify B
         enter.gameObject.GetComponent( name_of_the_script_you're_accesing ).name_of_the_variable_;
     }
 }
Comment
Add comment · Show 3 · 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 juliancruz87 · Jun 11, 2011 at 02:26 AM 0
Share

It does not work, I am trying to access the property and I always get an error that says life is not property in the class GameObject. I created my property so, public int life = 100, and the script is called PlayerSettings, but does not work, so I'm also trying to create public static int PLAYERLIFE = 100, but neither worked.

avatar image Joshua · Jun 11, 2011 at 03:01 AM 0
Share

Actually, I made a stupid mistake. The variable "enter" contains the Collider, not the gameObect. So ins$$anonymous$$d of enter.GetComponent... you want to use enter.gameObject.GetComponent...

avatar image ckfinite · Jun 11, 2011 at 03:30 AM 0
Share

And I made the same mistake too.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I access other scripts and their functions? 3 Answers

Can't find gameobjects being spawned getting a nullreferenceexception 1 Answer

Set variable in script from a different script... 3 Answers

Disable/Enable Script or Add/Remove? 1 Answer

Change position of GameObject between unity3d and eclipse 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