I have a building prefab. There is a simple script attached to it that allows me to store unique data for each prefab instance, such as address. There are hundreds of clones of this prefab.
The prefab is instantiated like so:
//instantiate a "building" object :
multiFamHigh = Instantiate(multifamilyHigh,myV3,Quaternion.identity) as GameObject;
//set a reference to the prefab's behavior script:
BuildingBehavior bldgBhave = multiFamHigh.GetComponent<BuildingBehavior>();
//set the information for the prefab:
bldgBhave.setInfo( mgr.lotsList[chose].getAddress(),buildingRef);
In the "TownCreator" script, after the prefab is instantiated, I call the setInfo function and pass the address to it. The setInfo looks like this:
public class BuildingBehavior : MonoBehaviour {
private string address;
public void setInfo(string useString, int bldgListRef)
{
address = useString;
Debug.Log ("Set Address = " + address);
buildingListRef = bldgListRef;
}
}
The debug log correctly displays "you set address 3071 N." - meaning at that time, the "address" variable of the prefab script is set to 3071 N. Perfect.
However; later, when I click on any of the prefabs at runtime (via the mousedown) the UI message box pops up (as intended), and the Text is changed from "New Text" (default) to a blank string. I added a similar Debug.Log, at that point, and it also tells me that "address" on the clone is blank.
That code is:
void OnMouseDown()
{
thisCG = GameObject.Find("MainCanvas").GetComponent<CanvasGroup>();
thisCG.alpha = 1;
CanvasStuff thisCS = GameObject.Find("MainCanvas").GetComponent<CanvasStuff>();
thisCS.setAddyText (address);
Debug.Log ("clicked on " + address);
}
Any ideas why the script on the prefab clones is not storing the values?
Also - when I change (address) to ("Address"), the popup has "Address" above the OK, Thanks! button. :/
setAddress()
. However, I suspect the reason things aren't working is because prefabs explicitly don't store runtime changes after the run has terminated. \$\endgroup\$