- Home /
Changing Prefab Variables From Another Script In Editor
Hey,
I have a prefab with script X on it variable XX. I also have a script Z with the variable ZZ on another object(not the prefab). While in the editor I want to be able to change variable XX to ZZ. So what ever ZZ is XX always equals on the prefab. I also want to use script X on more than one prefab with different XX values. If im not clear about what im asking please let me know so I can clarify...Thx
~Wakeful
So you want to access script X which is on a prefab from a script attached to an object in the scene?
Yes that is correct. Script X is on a prefab and Script Z is on a gameObject in the scene.
Answer by abi-kr01 · Jan 20, 2015 at 05:12 PM
Here is something that i have done for my projects it might help you as well
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Z : MonoBehaviour {
public int zz; //varible zz on main script
void Start ()
{
update_value ();
}
void update_value ()
{
//finding all gameobjects that are avail on scene
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
zz=Random.Range(0,100);
if (obj.transform.tag == "prefab") // finding and adding new value in xx with new zz for each gameobjects with prefab tag
{
print("Before update "+obj.GetComponent<X>().xx);
obj.GetComponent<X>().xx=zz;
print("After Update"+obj.GetComponent<X>().xx);
}
}
}
}
here is the script that is attach to all prefab
using UnityEngine;
using System.Collections;
public class X : $$anonymous$$onoBehaviour {
public int xx;
}
Close but not what im looking for, but thanks tho. I want to be able to update the prefab before I run the game. What im doing is only going to be for when im in the editor anyways, but it would make it a lot easier for me when Im creating alot of prefabs and all have then in a list.
the "Z" script is of type "mono behavior" you can use "EditorWindow" and modify script to do the same task