- Home /
How do I control the transform.position of an instantiated prefab?
I am trying to make a road that will spawn itself as my player runs along it. Currently I have this:
var spawnThis : Transform;
function OnTriggerEnter (other : Collider)
{
Instantiate (spawnThis, transform.position, transform.rotation);
Destroy (gameObject);
}
it's the Instantiate line I am having trouble with, it just spawns it in the same place as the first object was, I want it to spawn at the next position.x integer. I was hoping something like this:
Instantiate (spawnThis, transform.position.x ++, transform.rotation);
except that returns this error:
No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, float, UnityEngine.Quaternion)' was found.
How can I go about this?
Answer by Cr0ss0vr · Jul 10, 2013 at 09:46 AM
I think i'd have just made a variable and stored the instantiated game object in it, that way you can just call the variable and do what you want with it, transform.position, or even rigidbody stuff...
var spawnThis : Transform;
var newSpawnThis;
var speed;
function OnTriggerEnter (other : Collider)
{
newSpawnThis = Instantiate (spawnThis, transform.position, transform.rotation);
newSpawnThis.position.x += speed;
Destroy (gameObject);
}
that should work, right?
not a problem... i should have probably commented it, cause i dont really remember whats going on right here... i'm guessing spawnThis is the original object, and when something hits a collider its instantiated into newSpawnThis... not too sure whats getting destroyed though...
How should I change this line to transform position to the other gameobject that is not in the array :
void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,Quaternion.identity); }
Something like this:
void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,spawn.transform.position); }
Answer by robertbu · Jul 10, 2013 at 03:08 AM
You can do something like this:
function OnTriggerEnter (other : Collider)
{
var v3 = transform.position;
v3.x += distance;
Instantiate (spawnThis, v3, transform.rotation);
Destroy (gameObject);
}
...where 'distance' is either a variable you define or a hard-coded value.
I tried something like that, except it gives me this:
"You are not allowed to call get_transform when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration."
and I am like "Whaaaaaaaaaaaaaaaat?"
How else do i store the position to a variable? :/
The code above compiles for me, but I've seen this error before. Typically I would take this:
var v3 = transform.position;
...and change it to this:
var v3 : Vector3;
v3 = transform.position;
...in order to get rid of the error.
drats, that returned:
No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, System.Type, UnityEngine.Quaternion)' was found.
welp this was 4 years ago... looking back at beginner me makes me wanna smack me.
You need to specify the type. You cannot do:
var v3;
You need:
var v3 : Vector3;
Your answer
Follow this Question
Related Questions
Setting parent of instantiated sprite 2 Answers
How do you instantiate an object to be oriented along a vector3? 1 Answer
Can't remove instantiated prefab 0 Answers
Variables stay overwritten in inspector 1 Answer
Bullet Translation question 0 Answers