- Home /
Unable to assign prefabs target at runtime
I'm currentley trying to assign the target of a script attached to one of my prefabs, the idea being that i will be able to spawn the prefab and its script will grab its target. The prefab is set as the child of an empty gameobject, which i'm using to assign the target variable of the prefab with this piece of code:
function Awake ()
{
transform.Find("tornadogameobject").GetComponent("newgrav").varplanet = "Sphere";
}
The Gameobject "Sphere" is permanently in the scene hierarchy. I'm really struggling to figure out why the "Sphere" Gameobject currently isn't being assigned as th target when the game is running and i can't think of a differnet way to do it considering i can't assign the prefabs target before its spawned. If anyone could show me were i'm going wrong i'd be extremely gratefull, been struggling with this for a couple of days now.
Edit:
here's the code i will be using for the spawning:
function OnTriggerEnter(other:Collider)
{ var spawn:SpawnPoint = other.GetComponent(SpawnPoint);
if(spawn) spawn.DoSpawn(); }
and heres the code i am using to make the tornadogameobject prefab rotate around the Sphere
var rotationSpeed = 120.0;
var translationSpeed = 10.0; var height = 2.0; //height from ground level private var centre : Transform; //transform for planet private var radius : float; //calculated radius from collider var planet : SphereCollider; //collider for planet
function Start ()
{ //consider scale applied to planet transform (assuming uniform, just pick one) radius = planet.radius * planet.transform.localScale.y; centre = planet.transform; //starting position at north pole transform.position = centre.position + Vector3(0,radius+height,0); }
function Update ()
{ //translate based on input var inputMag = Input.GetAxis("Fire1")translationSpeed*Time.deltaTime; transform.position += transform.forward inputMag; //snap position to radius + height (could also use raycasts) targetPosition = transform.position - centre.position; var ratio = (radius + height) / targetPosition.magnitude; targetPosition.Scale(Vector3(ratio, ratio, ratio) ); transform.position = targetPosition + centre.position; //calculate planet surface normal surfaceNormal = transform.position - centre.position; surfaceNormal.Normalize(); //GameObject's heading var headingDeltaAngle = Input.GetAxis("Fire2") Time.deltaTime rotationSpeed; headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up); //align with surface normal transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) transform.rotation; //apply heading rotation transform.rotation = headingDelta transform.rotation; }
Answer by benni05 · Apr 25, 2011 at 05:10 PM
Hm. I do not fully understand your description, but what your script actually is doing is to assign the String value "Sphere" to a variable (varplanet) defined in a script(?) called "newgrav" that is attached as a component to an object called "tornadogameobject". Is that correct?
I see no GameObject "Sphere" here, maybe this is the confusion? What are you calling a "target"? Would you like to assign a reference of the GameObject "Sphere" to the variable varplanet? Then you should do:
var sphere = GameObject.Find("Sphere");
transform.Find("tornadogameobject").GetComponent("newgrav").varplanet = sphere;
Hope this helps
Ben
UPDATE
Based on the discussion below in the comments it should be "...planet = sphere" and not varplanet:
The variable planet requires a SphereCollider which hopefully is attached as a component to your planet.
So the assignment should be:
var sphereColl : SphereCollider;
sphereColl = GameObject.Find("Sphere").GetComponent(SphereCollider) as SphereCollider;
transform.Find("tornadogameobject").GetComponent("newgrav").planet = sphereColl;
Ben
you just explained what i was trying to say a lot better than i did, thats basically what i'm trying to do, as a way giving my prefab (tornadogameobject) a scene reference when it spawns. The script i posted is attached too the parent of the prefab. I think i understand what your doing with your script, but it's still giving me a variable i would have to assign in the inspector, which brings me back to the issue of prefabs being unable to reference gameobjects in scene. Essentially i've been trying to find a way get the prefab to get its target (Sphere) when it spawns in the scene.
also its giving me a very strange error message, "UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration." any idea why thats happening?
Those variables you can assign Prefabs to in the inspector are public variables (i.e. in Javascript: public var myPrefab;). Those are assigned via Drag&Drop normally. This is how you presumably referenced the Prefab from the parent object's script. Very important to understand here is that a Prefab is just an asset. Once it is instantiated it becomes a GameObject (usually it is named then "PrefabName(Clone)". This GameObject behaves like all other GameObjects and you can of course search and assign Object references as you like. Those variables to store them need not to be public.
Before I continue guessing... could you please post the code fragments crucial here? Instantiation of the prefab for example and also explain again what you want to achieve by assigning the "target" sphere to the instantiate prefab? What is the purpose of this?
yeah of course i'll edit my post now and put them in. The Sphere is my game playing area (think mario galaxy style walking on planets). The prefab is tornado that when spawned will chase the player around the planet. For the prefab to this correctly it needs to be under the influence of the fauxgravity script ( i called it newgrav) to allow it to move around the sphere. without this working it drops of into oblivion. So when the prefab spawns i need it to reference "Sphere". I haven't fully implenented the spawning code yet, as i still don't know if my prefab can can reference the variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125213im_/https://answers.unity.com/themes/thub/images/avi.jpg)