In my game I have a script that instantiates objects
var object:Transform;
var moverprefab: Transform;
...
object = Instantiate(moverprefab, Vector3(1025.872, 41.44104, 2492.541),Quaternion.Euler(0,29.22,0-16.8)); //first mover
var theScript = object.GetComponent(lvl4padmover);
theScript.targetA = top.gameObject;
theScript.targetB = bottom.gameObject;
The mover prefab is one of my prefabs with a default mover script…
var targetA : GameObject;
var targetB : GameObject;
var speed : float = 0.1;
function FixedUpdate ()
{
var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5; transform.position = targetA.transform.position * weight + targetB.transform.position * (1-weight);
}
My problem is I assign the variables during runtime in my script but i get reference exception error during runtime yet the scripts properties are being assigned after I instantiate my prefab?
As you can see in the video below my moving pad is the one with the script and its moving with the values assigned yet i get the error causing game stuttering? any way to fix this?
Try:
var myobject:GameObject;
var moverprefab: GameObject;
myobject = (GameObject) Instantiate(moverprefab, Vector3(1025.872, 41.44104, 2492.541),Quaternion.Euler(0,29.22,0-16.8)); //first mover
var theScript: lvl4padmover = (lvl4padmover ) myobject.GetComponent(lvl4padmover);
I have tried this and I get an insert semicolon at the end error. This compiles if i remove the (GameObject) cast and (lvl4padmover) cast but this then causes other errors.
When i was just trying to get it to compile I wrapped the everything being casted and resulted in this error
BCE0024: The type ‘UnityEngine.GameObject’ does not have a visible constructor that matches the argument list ‘(UnityEngine.GameObject)’.
myobject = (GameObject)(Instantiate(moverprefab, Vector3(1025.872, 41.44104, 2492.541),Quaternion.Euler(0,29.22,0-16.8))); //first mover
var theScript:lvl4padmover=(lvl4padmover)(myobject.GetComponent(lvl4padmover));
I got the prefab to load correctly by using a If statement to not run the mover script without references being assigned which then turns true when i change the references. BUT I now get a huge frame drop from the main thread.