- Home /
Script not using other script #C
I have a script where on collision it set itself to false. This works fine, but when I try to tell it to call Emotes.Hearts(); from my Emote script I get this error:
Assets/Scripts/Emotes.cs(11,21): error CS1624: The body of `Emotes.Hearts()' cannot be an iterator block because `void' is not an iterator interface type
Here's my class attached to Fruit:
using UnityEngine;
using System.Collections;
public class Fruit : MonoBehaviour {
private Emotes emotes;
void Start(){
_HpMax = 200;
_HpCur = 1;
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player"){
gameObject.SetActive(false);
emotes.Hearts();
}
//Destroy(gameObject);
}
}
}
And here's a bit of my Emotes (took all ten different types out to save on room, set up the same for each.)
using UnityEngine;
using System.Collections;
public class Emotes : MonoBehaviour {
public GameObject _floatinghearts;
void Awake(){
_floatinghearts.active = false;
}
public IEnumerator Hearts(){
_floatinghearts.active = true;
yield return new WaitForSeconds (1);
_floatinghearts.active = false;
}
}
Answer by robertbu · Feb 21, 2014 at 04:43 PM
Change line 15 int the first script to:
StartCoroutine(emotes.Hearts());
Thanks, I got that changed. Yet, when I run it I gives me this error now:
NullReferenceException: Object reference not set to an instance of an object
Fruit.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Fruit.cs:10)
you need to assign emotes first, like:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player"){
gameObject.SetActive(false);
emotes = other.gameObject.GetComponent<Emotes>(); // if Emotes script is put on "other" gameObject
emotes = GetComponent<Emotes>(); // if Emotes is put on this gameObject
StartCoroutine(emotes.Hearts());
}
Answer by CodeElemental · Feb 21, 2014 at 04:48 PM
You need to be more careful about the closing brackets the Fruit class has more closing brackets , just delete the one at the end (if it was not copy/paste typo)
Also , you need to call Hearts() as a Coroutine
StartCoroutine(emotes.Hearts());
Thanks, I got that changed. Yet, when I run it I gives me this error now:
NullReferenceException: Object reference not set to an instance of an object
Fruit.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Fruit.cs:10)