I am getting this error
Assets/_scripts/spawn_cube.cs(29,13): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Here is my script
using UnityEngine;
using System.Collections;
public class spawn_cube : MonoBehaviour {
public GameObject cube;
public float spawn_position;
public float timer = 0.0f;
spawn_cube()
{
Vector3 spawn_position = new Vector3(0, 0, 0);
GameObject temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity) as GameObject;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer > 10)
{
spawn_cube();
timer = 0;
}
}
}
Well, wrong thing with this script is that you’re calling this function spawn_cube() which is a constructor. you don’t need to call a constructor it’s called when objects is initialised. But it’s not going to give you desired results. you need to change the function name and call it in order to instantiate game objects.
Good luck
Hi. Try this. Note that I’m using Time.time
and not Time.deltaTime
. Time.deltaTime
is the time between the last frame and the current frame. Time.time
is the actual time in seconds since the program started.
using UnityEngine;
using System.Collections;
public class CubeSpawner: MonoBehaviour {
public GameObject cube;
public float spawn_position;
public float timer = 0.0f;
void SpawnCube() {
Vector3 spawn_position = new Vector3(0, 0, 0);
GameObject temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity) as GameObject;
}
void Start () {
timer = Time.time;
}
void Update () {
if (Time.time - timer > 10) {
SpawnCube();
timer = Time.time;
}
}
Note that this will spawn the GameObject in the same spot every time.