1
\$\begingroup\$

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);
    }

This happens:enter image description here

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. :/

\$\endgroup\$
  • 2
    \$\begingroup\$ We need more information, I fear. Show the code that you use to instantiate and call 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\$ – Draco18s Jun 21 '17 at 21:05
  • \$\begingroup\$ Thanks for the edit and reply. I can post more later - but I am not hoping for the prefab to store information from run to run, rather within a single "play" :( \$\endgroup\$ – scottwinch Jun 21 '17 at 21:07
  • 1
    \$\begingroup\$ I was unsure. Even so, I need to see that section of code to determine what you may be doing wrong. \$\endgroup\$ – Draco18s Jun 21 '17 at 21:09
  • 2
    \$\begingroup\$ Personally I'd want to see more context around your code because I'm pretty sure you are making a mistake like these are actually two different values which are both called "address". \$\endgroup\$ – jhocking Jun 21 '17 at 21:10
  • \$\begingroup\$ @jhocking - I totally understand where you are coming from and will edit my post later tonight. The setAddress() and mouseDown() functions both reside in the same script on the prefab and both use the same "address" variable. I will also add the instantiation and setAddress call that Draco18s asked for earlier. \$\endgroup\$ – scottwinch Jun 21 '17 at 21:29
0
\$\begingroup\$

The problem was that my prefab was done in haste as a place holder until other assets were further refined and both the plane and the cube that made up the prefab had the script attached to it. The address was being stored on the plane during creation and the onMouseDown() was being triggered on the cube.

So, in a way @jhocking was right that two different "address" variables were being used.

sigh - live and learn :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.