Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

why cant i call this function?

$
0
0
ok so ive been working at this for ages and I can't see whats wrong with it. When the heart object collides with the player its supposed to call a function from the GameManager script to increase his health, but it wont call the function. I've tried "SendMessage" and using gameManager.increaseHealth(); but nothing works please help. using UnityEngine; using System.Collections; public class heart : MonoBehaviour { //here is a private object that we'll set as the player in start private GameObject target; public GameManager gameManager; void Start () { //once this script starts (when a heart exists) we find the player so we can have it move towards the player target = GameObject.Find("Player"); //here we add a bit of force at the beginning to help give the heart a nice little animation once it is spawned rigidbody.AddForce(Random.Range(-600,600),0,Random.Range(-600,600)); } void Update () { //before we do anything, we just check to see if the target is not null. this helps avoid any unwanted errors if(target != null){ //here we check the square distance so we can see how close the player is to the object. var sqrDistance = (target.transform.position - transform.position).sqrMagnitude; //if the square distance is less than 16, we'll let the heart do stuff. if (sqrDistance <= 16){ //before we can get the heart to move towards the player, we want to find the direction var dir = target.transform.position - transform.position; dir = dir.normalized; //now we apply force towards the player using the variable dir (aka direction). rigidbody.AddForce(dir * 1500 * Time.deltaTime); } } } void OnTriggerEnter (Collider other) { //if the tag of the trigger is in fact heart, we do stuff. if(other.tag == "Player"){ //we destroy the heart, //gameManager.SendMessage("increaseHealth", SendMessageOptions.DontRequireReceiver); gameManager.increaseHealth(); Destroy(gameObject); Debug.Log("heart collided"); } } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>