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 /
  • Help Room /
avatar image
0
Question by CRISTALSONIC · Jan 20, 2017 at 11:05 PM · syntax

how can I have a game object get a variable from a different object with collision

so i have a basic player and when the hit a pick up (health or damage) I want then to get the value or val variable off the object to pass into this function on there script

 void OnCollisionEnter(Collision other) {
         //if (other.gameObject.CompareTag ("Damage")) {
 
         if (other.gameObject.name=="Damage"){
             //GameObject timp= other.collider.gameObject.GetComponent (HP_Val);
             HP_fill.fillAmount = HP.SetHP (**"How do I get Val here?"**);
             HPtext.text = HP.desplay_HP;
             //Destroy (other.gameObject);
             if (HP.Helth == 0) 
             {
                 m_Animator.SetBool("KO", true);
             }
         }
     }

I know this is do able but what am I missing?

Comment
Add comment · Show 1
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 CRISTALSONIC · Jan 21, 2017 at 07:48 PM 0
Share

thanks for the help

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HenryStrattonFW · Jan 20, 2017 at 11:46 PM

lets assume that your damage/health pack classes are called "DamagePack" and "HealthPack" and each has a public variable called "value". Here is how you could test for and get those variables.

         // Try to find components on the object.
         HealthPack lHealthPack = other.gameObject.GetComponent<HealthPack>();
         DamagePack = lDamagePack = other.gameObject.GetComponent<DamagePack>();
 
 
         if (lHealthPack != null)
         {
             // you have a health pack just use lHealthPack.value to increase health.
         }
         if (lDamagePack != null)
         {
             // you have a damage pack just use lDamagePack.value to decrease health.
         }
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 CRISTALSONIC · Jan 21, 2017 at 02:26 AM 0
Share

well that did help its not giving me an error wen i try to pass temp.Val into setHP


 void OnCollisionEnter(Collision other) {
     //if (other.gameObject.CompareTag ("Damage")) {

     if (other.gameObject.name=="Damage"){
         HP_Val temp= other.collider.gameObject.GetComponent (HP_Val);
         //int x= temp
         HP_fill.fillAmount = HP.SetHP (temp.Val);
         HPtext.text = HP.desplay_HP;
         //Destroy (other.gameObject);
         if (HP.Helth == 0) 
         {
             m_Animator.SetBool("$$anonymous$$O", true);
         }
     }
 }

I do have a different error that is probably me just over looking something obvious

Assets/script/characterController.cs(99,57): error CS0118: HP_Val' is a type' but a `variable' was expected

avatar image HenryStrattonFW CRISTALSONIC · Jan 21, 2017 at 04:06 PM 0
Share

This is because your type is (im assu$$anonymous$$g) HP_Val but when you're trying to use GetComponent you have to do it in certain ways. you can either use the type name as a string "HP_Val" as a System.Type "typeof(HP_Val)" or as a generic "GetComponent()" note the angle brackets in that last one.

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

The documentation of the GetComponent method should help clear this up and get you back on track.

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

89 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

Related Questions

Assigning Values In Start () That Will Change 1 Answer

Ray-cast only detecting tags in debug.log and not in a if statement 0 Answers

Line renderer how to create and edit 1 Answer

How to use the predefined syntax in monodevelop quickly ? 1 Answer

How can I get this code to recognize coll in my update function? 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