- Home /
Can't assign variable to prefab, or apply changes to prefab.
just like i said in the question. I have a game object im trying to track in a script, i declared a public variable with the intention of dragging and dropping the game object into the script that was attached to a prefab - but it didn't allow me to assign it. Then i put the prefab in the scene and again tried to assign the variable. This time it let me drag the object in, however it wouldnt let me apply changes. Heres the script
using UnityEngine;
using System.Collections;
public class Planet : MonoBehaviour {
public float size;
public float speed;
public float speedMin;
public float speedMax;
public Vector3 position;
public double mass;
public GameObject pivot;
public GameObject planetMenuRoot;
void Start()
{
speed = Random.Range(speedMin, speedMax);
}
void OnTriggerEnter()
{
Debug.Log ("planet entered");
planetMenuRoot.SetActive(true);
AstraeusFlight.inPlanet = true;
AstraeusFlight.planet = gameObject.transform;
}
void Update()
{
transform.RotateAround(Vector3.zero, Vector3.up, speed * Time.deltaTime);
}
}
the variable im attempting to assign is the planetMenuRoot which is an empty game object inside of an NGUI panel. I have also tried spawning a cube and an empty game object and attaching those to the script on the prefab - no luck. anyone know whats going on?
I'm afraid you haven't made it terribly clear what you tried that didn't work.
I'll see if i can explain more clearly. I wrote the script i posted above which has a public variable in it - planet$$anonymous$$enuRoot. I then made a prefab that has that script attached to it. then i selected the prefab from the project panel such that i could see all of its components in the inspector window. then i attempted to drag and drop the planet menu root game object into the planet$$anonymous$$enuRoot variable slot in the planet script of the prefab. this didn't work - the variable simply didnt assign.
Then i dragged the prefab into the scene. then i selected the prefab in the scene hierarchy such that i could see all of its components in the inspector window. then i attempted to drag and drop the planet menu root game object into the planet$$anonymous$$enuRoot variable slot in the planet script of the prefab. this time the variable assigned just as it was supposed to. when i hit "apply" in the inspector window - in the hopes of applying this change to all prefabs - it didnt apply the changes and ins$$anonymous$$d the prefab in the project view remained without the assigned variable.
Lastly i tried dragging the prefab in the scene over the prefab in the project panel in the hopes of over-writing with a version of the prefab that had the variable assigned. This still did not work.
Answer by TSI25 · Jul 20, 2014 at 09:02 PM
looks like i can only assign prefabs from the project panel, or children of the game object that the script is attached to. this is way more restrictive than i remember assigning public variables to be but i guess these are parameters i can work with.
Your answer
Follow this Question
Related Questions
Cloned/Instantiated GameObject's Losing Public Variables When Created? 1 Answer
Spawn Prefabs 1 Answer
Creating an array of prefabs? 4 Answers
Replacing empty gameobjects with prefabs 3 Answers