- Home /
The question is answered, right answer was accepted
SendMessage GatherEnergy has no receiver
Can someone explain to me why this setup I have is incorrect. I believed I had used the SendMessage method correct but the compiler is yelling at me that its not. Thank you for your time.
Here is the two codes I am using:
The first one.
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 10.0f;
private float currentHealth;
public GameObject deathEffect;
public GameObject damageEffect;
public string collisionTag = "PlayerBullets";
public int theEnergy = 20;
private GameObject secondFormManager;
// Use this for initialization
void Start ()
{
currentHealth = maxHealth;
secondFormManager= GameObject.FindGameObjectWithTag ("SecondFormManager");
}
void OnTriggerEnter(Collider col)
{
if(col.tag == collisionTag)
{
currentHealth -= 1.0f;
Instantiate(damageEffect,col.ClosestPointOnBounds(transform.position), Quaternion.identity);
if(currentHealth <= 0)
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
//THE SENDER CODE
secondFormManager.SendMessage ("GatherEnergy", 20.0f);
}
}
}
}
The second one.
using UnityEngine;
using System.Collections;
public class ChangeToSecondForm : MonoBehaviour
{
private static ChangeToSecondForm instance = null;
public static ChangeToSecondForm Instance
{
get {return instance; }
}
private GameObject playerToBeChangedFrom;
private GameObject playerChangingTo;
public Transform secondFormDisplay;
public float maxEnergy = 100.0f;
public float currentEnergy;
private float energyOriginalXScale;
// Use this for initialization
void Start ()
{
currentEnergy = 0.0f;
energyOriginalXScale = secondFormDisplay.localScale.x;
playerToBeChangedFrom = GameObject.FindGameObjectWithTag ("PlayerCharacter");
playerChangingTo = GameObject.FindGameObjectWithTag ("PlayerCharacterSecondForm");
playerChangingTo.SetActive (false);
}
// Update is called once per frame
void Update ()
{
if (currentEnergy <= 0)
{
secondFormDisplay.GetComponentInChildren<Renderer>().enabled = false;
}
SecondForm ();
}
public void SecondForm()
{
if (Input.GetButtonDown ("TransformToSecond") && (currentEnergy == 100))
{
Debug.Log("Q has been pressed");
currentEnergy = 0;
playerToBeChangedFrom.SetActive (false);
playerChangingTo.SetActive(true);
playerChangingTo.SendMessage ("TransformQueSound", true);
Invoke ("ChangeBack", 10.0f);
}
}
public void ChangeBack ()
{
playerToBeChangedFrom.SetActive (true);
playerChangingTo.SetActive (false);
}
//THE RECEIVING CODE
public void GatherEnergy(float energyGathered)
{
Debug.LogError ("Got Energy from dead enemy.");
currentEnergy += energyGathered;
if (currentEnergy >= 100)
{
currentEnergy = 100.0f;
}
secondFormDisplay.localScale = new Vector3(energyOriginalXScale * (currentEnergy/maxEnergy), secondFormDisplay.localScale.z, secondFormDisplay.localScale.z);
}
}
I didn't think that the alternateAttack$$anonymous$$anager would need the script attached. I'll get home to do it and see what I can bring to light from there. UPDATE: I looked at the code one more time to see why you said the thing you did and now I see I have made an incredibly bad typo. This was a silly mistake. Thanks for helping anyhow.
That's most likely the problem. I'll post it as an answer . Let me know if it works.
It works better now but I was only able to progress a little. I wasn't sure if I'm allowed to ask two separate questions in one thread because I don't remember from where but something stated a thread was to keep one question in a thread. So the updated script and question is in this link in another thread.
Answer by LessThanEpic · Jan 23, 2015 at 11:29 PM
Have you verified that your alternateAttackManager game object has the ChangeToSecondForm script attached?