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.
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);
}
}