Hello im very new to programming, i want to put my instantiate vector position at z-5 of the gameobject, so i will be able to use same script for multiple objects. here is my code:
private var x=transform.position.x;
private var y=transform.position.y;
private var z=transform.position.z-5;
var tasspeed:float=5;
var tas: GameObject;
function Update(){
var waitTime : float = Random.Range(0.0,30.0);
yield WaitForSeconds(waitTime);
Instantiate(tas,Vector3(x,y,z),Quaternion.identity);
tas.rigidbody.AddForce(transform.up*tasspeed,ForceMode.VelocityChange);
}
This code should not even compile: you can’t initialize a variable with game object properties, nor use yield inside Update - using yield in a function makes it a coroutine, and periodic functions like Update can’t be coroutines.
If you want to create the tas object only once after a random delay, use this:
var tasspeed: float = 5;
var tas: GameObject;
function Start(){ // Start can be a coroutine
var waitTime : float = Random.Range(0.0,30.0);
yield WaitForSeconds(waitTime);
var pos: Vector3 = transform.position; // get the position
pos.z -= 5; // apply the desired offset
Instantiate(tas, pos, Quaternion.identity);
tas.rigidbody.AddForce(transform.up*tasspeed,ForceMode.VelocityChange);
}