- Home /
C# Return Type Error?
I have studied this code overhand over but can't figure out what's wrong. The Error that i'm getting says : The body of CameraScript.SpawnBall()' cannot be an iterator block because
void' is not an iterator interface type (This error is referring to the line where 'public void SpawnBall' is declared.
Here is the script :
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
//Follow The Ball Variables
private Vector3 _CameraPosition;
public float CameraHeight;
public Transform Ball;
//Checkpoint Variables
private LayerMask layerMask = 1 << 8;
private bool Fell = false;
public Transform StartingPoint;
public Transform CheckPoint_Explosion;
private Vector3 RevertToPoint;
public bool ResetCheckPoint = false;
public void SpawnBall()
{
yield return new WaitForSeconds(2.5);
Application.LoadLevel(Application.loadedLevelName);
RevertToPoint = new Vector3(PlayerPrefs.GetFloat("Ball_Revert_Position_X") , PlayerPrefs.GetFloat("Ball_Revert_Position_Y") , PlayerPrefs.GetFloat("Ball_Revert_Position_Z"));
Ball.transform.position = RevertToPoint;
}
// Use this for initialization
void Start ()
{
layerMask = ~layerMask;
if(ResetCheckPoint)
{
PlayerPrefs.SetFloat("Ball_Revert_Position_X" , StartingPoint.transform.position.x);
PlayerPrefs.SetFloat("Ball_Revert_Position_Y" , StartingPoint.transform.position.y);
PlayerPrefs.SetFloat("Ball_Revert_Position_Z" , StartingPoint.transform.position.z);
}
RevertToPoint = new Vector3(PlayerPrefs.GetFloat("Ball_Revert_Position_X") , PlayerPrefs.GetFloat("Ball_Revert_Position_Y") , PlayerPrefs.GetFloat("Ball_Revert_Position_Z"));
Ball.transform.position = RevertToPoint;
}
// Update is called once per frame
void Update ()
{
if(!Fell)
{
_CameraPosition.x = Ball.position.x;
_CameraPosition.y = CameraHeight;
_CameraPosition.z = Ball.position.z;
}
this.gameObject.transform.position = _CameraPosition;
//Detect Ball Off Level
RaycastHit Hit;
Vector3 DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * (CameraHeight + 750) , Color.red);
if(!Physics.Raycast(transform.position , DirectionRay , out Hit , (CameraHeight + 750) , layerMask))
{
Debug.Log("Fell");
Fell = true;
}
if(Physics.Raycast(transform.position , DirectionRay , out Hit , (CameraHeight + 750) , layerMask))
{
if(Hit.transform.tag == ("BombPlateSite"))
{
Instantiate(CheckPoint_Explosion , Hit.transform.position , Quaternion.identity);
PlayerPrefs.SetFloat("Ball_Revert_Position_X" , Hit.transform.position.x);
PlayerPrefs.SetFloat("Ball_Revert_Position_Z" , Hit.transform.position.z);
PlayerPrefs.SetFloat("Ball_Revert_Position_Y" , (Hit.transform.position.y + 5));
RevertToPoint = new Vector3(PlayerPrefs.GetFloat("Ball_Revert_Position_X") , PlayerPrefs.GetFloat("Ball_Revert_Position_Y") , PlayerPrefs.GetFloat("Ball_Revert_Position_Z"));
Debug.Log("CheckPoint");
}
}
//Handle What Happens When The Ball Falls
if(Fell)
{
Ball.transform.position.y -= 10;
SpawnBall();
}
}
}
Any help is appreciated, thanks.
Answer by Bunny83 · Feb 01, 2012 at 11:20 PM
It seems that SpawnBall should be a coroutine. A coroutine always have to return an IEnumerator. Your return type is void so that won't work.
Also to call a coroutine you have to use StartCoroutine(SpawnBall())
;
edit
Coroutines are a .NET / CIL feature. Another name is generator or generator-function. Such a function returns a special object (which implements the IEnumerator interface) which can be used to iterate through the yields inside the function. This iteration is done by Unity's coroutine scheduler. If you just call SpawnBall()
it will create such an object, but no body is using it. You have to hand over the object to StartCoroutine() which will enqueue it into Unity's internal coroutine manager.
There has been a lot questions about coroutines on this site. Just look them up if you want to know more about that topic. If you have more specific questions on a certain issue, feel free to ask a new question.
Your answer
Follow this Question
Related Questions
Get enum value out of function c# 2 Answers
Why inpector keeps methods assigned when changed from public to private 2 Answers
How can I stop executing a Function in a void Function? 1 Answer
code path returning error 2 Answers
private Vector3 function? 2 Answers