0
\$\begingroup\$

I'm creating a script to dynamicly load a menu with buttons. On every button there is a script that need to be run when the button has been clicked. For the button there is a prefab where the script in enabled on. After instantiating the prefab the script on it has been disabled. When I manually enable it by code it does nothing. When enabling the script using the editor it does wor.

My code for generating the buttons on my menu looks like:

 foreach (BuildingModel model in models)
    {
          GameObject tempButton = Instantiate(buttonPrefab);

          PlaceObjectToScene script = tempButton.GetComponent<PlaceObjectToScene>();

           script.objectName = model.modelLocation + "/" + model.modelName + ".fbx";

           script.gameObject.SetActive(true);
            script.enabled = true;

            tempButton.transform.SetParent(horizontalLayoutGroup.transform);

    }

I searched on the internet for some answers and found forum thread with the same issue but non of the sollutions works in my situation. Can someone help me how to enable the script that is default enabled on the prefab?

\$\endgroup\$
  • \$\begingroup\$ 1. Check if PREFABS have script enabled (not the objects on the scene). 2. Check if you have any errors in the console. If there's an error in the script, it will become disabled. \$\endgroup\$ – Jacob Jun 18 '17 at 18:52
  • \$\begingroup\$ Script is enabled in the prefab and did not got any errors in the script \$\endgroup\$ – JimmyD Jun 18 '17 at 19:03
  • \$\begingroup\$ Assets -> Export Package and upload it on Dropbox or somewhere. There's no error in this code, I'll take a look. \$\endgroup\$ – Jacob Jun 18 '17 at 19:04
  • \$\begingroup\$ You can find the assets here drive.google.com/file/d/0B7tTtE3e6xrTVERhMWMybE1ndWs/…. \$\endgroup\$ – JimmyD Jun 19 '17 at 16:00
  • \$\begingroup\$ It's about PreFab with name MenuButton. Script LoadSubMenu will initialize Prefab and PlaceObjectToScene script is in the prefab. \$\endgroup\$ – JimmyD Jun 19 '17 at 16:03
0
\$\begingroup\$

Ok found the issue. It was an error in my code. I had added a enabled = false in there. And is not the correct behaviour of my script.

\$\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.