- Home /
Reference other scrips in prefab
Hi there. I'm trying to create a script that displays text above all of the objects with the Tag "Building". So far, I've got a prefab and a script that instantiates that prefab for every building in the level, which works. The problem I'm having is that I want to assign each instance of the prefab to a building and display that text.
So what I'm asking is that I want to take a variable from one script, and put it into a variable inside a prefab, but I don't know how to access a script since I can't edit the inspector inside the prefab. Here's what I have:
buildingText.js
#pragma strict
var dupeScript : buildTextInst; // The script that contains the names and locations of the buildings.
private var cam : Camera;
private var build : GameObject;
function Start () {
//dupeScript = gameObject.Find("Building Text Obj").GetComponent("buildTextInst");
cam = dupeScript.displayCam;
build = dupeScript.building;
}
function Update () {
// Checks to make sure the chosen camera is enabled.
if(cam.enabled == true){
var screenPos : Vector3 = cam.WorldToScreenPoint (build.transform.position); // Translates the object's world position to screen position.
guiText.pixelOffset = Vector2(screenPos.x,screenPos.y);
guiText.text = build.name;
}
else
guiText.text = "";
}
buildTestInst.js
#pragma strict
var buildings : GameObject[]; // "Buildings" is the set of all buildings in the map
var building : GameObject; // Used by the loop in Start() to get all of the buildings
var prefab : Transform; // This is the object being duplicated
var displayCam : Camera; // This is the camera that is being used.
function Start () {
// Finds all of the Game Objects with the tag "Building" and inputs them into "buildings".
buildings = GameObject.FindGameObjectsWithTag ("Building");
// Runs through all of the buildings with the tag "Building".
for (var building : GameObject in buildings)
Instantiate(prefab,Vector3(0,0,0), Quaternion.identity);
}
function Update () {
}
So you want to make the instantiated object a children to the building object?
Answer by Mizuho · Jun 12, 2012 at 09:57 AM
for(var building : GameObject in buildings)
var clone : GameObject;
clone = Instantiate(prefab, Vector3(0,0,0), Quaternion.identity);
clone.transform.parent = building.transform;
clone.AddComponent("buildingText");
I apologize if my answer is a little wrong for the coding...I only use C# for Unity. I think I got what you were asking correct though...
sorry for not using the answer box, but i don't want to s$$anonymous$$l others' answers
It works to let me transfer variables, but it creates a component on every duplicate for every building, and also creates a component on the object that has buildTextInst attached to it.
Actually, it seems to be that it affects the prefab itself, making a messy cleanup job after I run it once.
@bslashingu: You mean that every time you run it, the prefab gets another copy of the component? Then maybe you should add Destroy(this.GetComponent("buildingText"));
in the OnApplicationQuit
event.