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 gamedevelopmenttsunami · Mar 11, 2017 at 12:13 AM · c#colliderscript.ontriggerenterlifebar

How to call an action from a script that is on the anonymous object you collide with?

I have two objects. Object A and object B. When object a enters object b's collider, I want object b to update one of its variables (lifepoints). Object a can be any of a number of objects and so it doesnt make sense for object b to have on it a script for dealing with every single thing. So instead object a when encountered increments the lifepoints down, but only when it has entered to other objects collider. My problem is mostly that the ontrigger enter part of my script is not working, and is doing nothing. Here is the script I have made already:

Basically I want lifepoints (really object health) to increment down when it collides with different things. So object b (the living thing) has this:

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;

 public class PlayerHealth : MonoBehaviour
 {
 public int startingHealth = 10;                           
 public int currentHealth;
 public GameObject explosion_prefab;
 bool isDead;                                               
 void Awake()
 {
     // Set the initial health of the player.
     currentHealth = startingHealth;
 }
 public void TakeDamage(int amount)
 {
     // Reduce the current health by the damage amount.
     currentHealth -= amount;
 }

 void Update()
 {
     if (currentHealth <= 0)
     {
         // ... it should die.
         Death();
     }
 }
     void Death()
 {
     // Set the death flag so this function won't be called again.
     isDead = true;

     Destroy(this.gameObject);
     GameObject bang = Instantiate(explosion_prefab, gameObject.transform.position, Quaternion.identity) as GameObject;

     Destroy(gameObject);
     Destroy(bang, 3); // delete the explosion after 3 seconds
 }
  }


So when the player/or object b collides with a rock or something (object a), object a will call playerhealth on object b and increment it down towards 0. here is my current code for object a (think of it like an asteroid you bump into in a spaceship and then take damage from).

using UnityEngine; using System.Collections;

public class PlayerHealthRemove : MonoBehaviour {

     public int attackDamage = 10;               // The amount of health taken away per attack.
     //public GameObject player;                          // Reference to the player GameObject.

     void Awake()
     {
     //currently blank
     }

     void OnTriggerEnter(Collider other)
     {
         PlayerHealth otherhealth = (PlayerHealth)other.gameObject.GetComponent(typeof(PlayerHealth));
         otherhealth.TakeDamage(10);
     }
  }


The problem is when I use this, takedamage is not being called correctly and the variable for lifepoints is not updated on object b. I tried putting the same code in a on update with the object b explicitly selected using public gameobject and -1 health per update, and it works. But it doesnt work from the on trigger enter action. What do I need to do to get this working? I'm guessing it has something to do with collider other not being what I think it is.

Thank you for any help.

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 gamedevelopmenttsunami · Mar 11, 2017 at 12:15 AM 0
Share

"collider other"

1 Reply

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

Answer by gamedevelopmenttsunami · Mar 11, 2017 at 09:19 PM

The problem was I should have used oncollision enter not on trigger enter. This was the answer:

  using UnityEngine;
  using System.Collections;

  public class PlayerHealthRemove : MonoBehaviour {


    
     public int attackDamage = 10;               // The amount of health taken away per attack.
     //public GameObject player;                          // Reference to the player GameObject.

  


     void Awake()
     {
     //currentl blank
     }

     void Update()
     {

     //OnTriggerEnter(other);

     }


 //void OnTriggerEnter(Collider other)
 void OnCollisionEnter(Collision other)
 {

     Debug.Log("start");
         PlayerHealth otherhealth = (PlayerHealth)other.gameObject.GetComponent(typeof(PlayerHealth));
     Debug.Log(other.gameObject.name);

     otherhealth.TakeDamage(10);
         

     
     }

}

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

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

Access variable from different class 1 Answer

Wheel rotation 1 Answer

OnTriggerEnter Issue - Collider problem 0 Answers

Unity 3D(C#) - How to connect more than two box colliders used as triggers? 1 Answer

I want that when my character enter into a room a video automatically play and when he leaves the room the video "turns off". 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