- Home /
Instantiate Problem ( RandomRange)
Hi. I need help . I get an error when I write these codes. Where do i make mistakes ? Thanks.
Error : CS1503 cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Transform
using UnityEngine; using System.Collections;
public class SpawnArea : MonoBehaviour { public Transform prefab; public float width = 3; public float second = 1; private bool create = true; void Update() { if (create) StartCoroutine("CreatePrefab"); }
IEnumerator CreatePrefab()
{
create = false;
width += width;
Instantiate(prefab, new Vector3(Random.Range(-2f,3f), 0, 0));
yield return new WaitForSeconds(second = Random.Range(1f, 5f));
create = true;
}
}
Answer by Vicarian · Jun 14, 2017 at 06:29 PM
The overload of Instantiate you're using expects an object of type Transform for its second argument. You'll need to supply a third argument - a Quaternion - to get this to do what you want.
using UnityEngine;
using System.Collections;
public class SpawnArea : MonoBehaviour {
public Transform prefab;
public float width = 3;
public float second = 1;
private bool create = true;
void Update() {
if (create)
StartCoroutine("CreatePrefab");
}
IEnumerator CreatePrefab()
{
create = false;
width += width;
Instantiate(prefab, new Vector3(Random.Range(-2f,3f), 0, 0), Quaternion.identity);
yield return new WaitForSeconds(second = Random.Range(1f, 5f));
create = true;
}
}
Your answer
Follow this Question
Related Questions
Instantiate a button at mouse click position, 0 Answers
How do I acces a specific GameObject within the terrain???? 0 Answers
Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer
How to Spawn after checking if the clones are destroyed. 1 Answer
Generate a new ball when the First Ball is release. 0 Answers