There’s a game object, which instantiates a prefab (enemy) in random time range (1-5)
it always shows me an error (sometimes two) like “phrasing error” and “Expecting ;”
Help me please
using UnityEngine;
using System.Collections;
var prefab : GameObject;
public class Spawner : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Instantiate (GameObject);
yield WaitForSeconds (Random.Range(1,5));
}
}
You don’t declare variables with “var” when using C#, you just use the type name followed by the variable name. Also, the variable needs to be declared inside the class, so you would have:-
public class Spawner : MonoBehaviour {
GameObject prefab;
...
The second problem is that you don’t use just “yield” in C#, you use “yield return”. Check out the manual page on the differences with C# for more information.
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject objEnemy;
// Use this for initialization
void Start () {
objEnemy = (GameObject) GameObject.FindWithTag ("Enemy");
}
// Update is called once per frame
void Update ()
{
SpawnPoint();
}
void SpawnPoint ()
{
Instantiate (Enemy);
yield return WaitForSeconds (Random.Range(1,5));
}
}
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
//var GameObject enemyPrefab
public GameObject objEnemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
objEnemy = Instantiate(objEnemy);
yeld return 0;
}
}
and it shows me
the trouble is the same: what’s the problem O_o
And how must it be?[/quote]
This error quite clearly states that. `void’ is not an iterator interface type, IEnumerator is.
Assets/Scripts/Spawner.cs(17,9): error CS1624: The body of Spawner.SpawnPoint()' cannot be an iterator block because void’ is not an iterator interface type