Hello, I’m relatively new to coding and could use the help of the more experienced.
This is the code I’m using for my 3rd person player for Pickups, Lives, Sounds such.
using UnityEngine;
using System.Collections;
public class CharacterScript : MonoBehaviour {
public int Energy=0;
public int Lives=5;
public AudioClip EnergyGet;
public AudioClip DeathScream;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Player Colliding With GameObjects
void OnControllerColliderHit(ControllerColliderHit hit){
//Death
if (hit.gameObject.tag=="Death"){
Lives--;
audio.PlayOneShot(DeathScream);
yield return new WaitForSeconds(audio.clip.length);
Application.LoadLevel(Application.loadedLevel);
}
//Pickups
if (hit.gameObject.tag=="Energy"){
Destroy(hit.gameObject);
audio.PlayOneShot(EnergyGet);
Energy++;
}
}
//Basic Score Lives
void OnGUI(){
GUI.Label(new Rect(0,10,100,100),"Energy:"+Energy.ToString());
GUI.Label(new Rect(0,100,10,0),"Lives:"+Lives.ToString());
}
}
This is the error I’m getting:
“CharacterScript.OnControllerColliderHit(UnityEngine.ControllerColliderHit)' cannot be an iterator block because void’ is not an iterator interface type”.
Usually I’d work out what was wrong by vaguely knowing what the error was referring to but this one stumps me. :S
Could someone explain to me what is wrong with the code and advise me on fixing it?
yield return new WaitForSeconds();
can only be used in an IEnumerator
i suspoect thats what the issue is
change that line to start corutine (someIEnumerator);
then add
IEnumerator someIEnumerator () {
yield return new WaitForSeconds();
}
“Assets/MyScripts/CharacterScript.cs(25,25): error CS1502: The best overloaded method match for `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)’ has some invalid arguments”
“Assets/MyScripts/CharacterScript.cs(25,25): error CS1503: Argument #1' cannot convert method group’ expression to type `System.Collections.IEnumerator’”
“Assets/MyScripts/CharacterScript.cs(43,35): error CS1729: The type UnityEngine.WaitForSeconds' does not contain a constructor that takes 0’ arguments”
Have I edited the code correctly?
using UnityEngine;
using System.Collections;
public class CharacterScript : MonoBehaviour {
public int Energy=0;
public int Lives=5;
public AudioClip EnergyGet;
public AudioClip DeathScream;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Player Colliding With GameObjects
void OnControllerColliderHit(ControllerColliderHit hit){
//Death
if (hit.gameObject.tag=="Death"){
Lives--;
audio.PlayOneShot(DeathScream);
StartCoroutine (someIEnumerator);
Application.LoadLevel(Application.loadedLevel);
}
//Pickups
if (hit.gameObject.tag=="Energy"){
Destroy(hit.gameObject);
audio.PlayOneShot(EnergyGet);
Energy++;
}
}
//Basic Score Lives
void OnGUI(){
GUI.Label(new Rect(0,10,100,100),"Energy:"+Energy.ToString());
GUI.Label(new Rect(0,100,10,0),"Lives:"+Lives.ToString());
}
IEnumerator someIEnumerator () {
yield return new WaitForSeconds();
}
}
IEnumerator someIEnumerator () {
yield return new WaitForSeconds(2.0F);
}
}
Because then I get another error I’ve never heard of…
“Assets/MyScripts/CharacterScript.cs(3,32): error CS0246: The type or namespace name `MonoBehaviour’ could not be found. Are you missing a using directive or an assembly reference?”