Question by
IshanCK · Apr 15, 2017 at 03:13 PM ·
c#errorerror messageerror-message
I Have an C# error and I can't fix it @username
In line "block = Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity) as GameObject;" has an error @username
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockSpawn : MonoBehaviour
{
public GameObject[] Blocks;
public Transform BlockSpawnPos;
public int BlockCount = 0;
public float NewPos;
private int randomNum;
private float waitTime = 2.0f;
// Use this for initialization
void Start ()
{
Block();
}
// Update is called once per frame
void Update ()
{
}
public void Block()
{
block = Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity) as GameObject;
Vector3 temp = BlockSpawnPos.position;
temp.y = 0;
temp.x += 5;
temp.z = 0;
BlockSpawnPos.position = temp;
StartCoroutine (wait());
}
IEnumerator wait(){
yield return new WaitForSeconds (waitTime);
BlockCount += 1;
Block ();
}
}
Comment
Best Answer
Answer by UnityCoach · Apr 15, 2017 at 03:24 PM
block has no type. You can use var as you assign it on the same line :
var block = Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity) as GameObject;
You can type it too
GameObject block = Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity) as GameObject;
Or, as you don't seem to use it later, you can just discard it
Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity);