- Home /
Button OnClick() function stops working after instantiating the prefab.
I'm working on a level editor system that uses an addon package called "SerializeHelper" (forgot the author's name, not my own work), and in my scene I have a Button that returns an integer to the Level Editor script so that it knows what block to place when the player clicks. When you save the game, details of prefabs in the scene as well as an ID get serialized and saved in a .mkr file. When you load the save, it basically clears the level of everything, and then instantiates the saved prefabs from a dictionary using the save's details.
In my debug scene, the button (with its return script), event system, and the level editor script itself all get saved and serialized. When I start the scene, my block ID is set to 0. I save the scene, then load it, and everything is there (yay!), but when I click on the button to change my ID, the integer doesn't change.
The buttons "OnClick()" parameters have the correct info, I have an EventSystem, and I even added a GetKeyDown to make sure I could change the ID (which works). It's like it just refuses to change the integer when clicked.
UI Return Code
using UnityEngine;
using System.Collections;
public class ReturnBlockID : MonoBehaviour {
LevelEditorPrototype levelEditor;
public void Start()
{
GameObject managerObject = GameObject.Find("GameManager");
levelEditor = managerObject.GetComponent<LevelEditorPrototype>();
}
public void BlockID(int blockID)
{
levelEditor.blockType = blockID;
}
}
Level Editor Code
using UnityEngine;
using System.Collections;
public class LevelEditorPrototype : MonoBehaviour
{
public GameObject id97; //Stone Floor
public GameObject id214; //Water
public int blockType;
public int castX;
public int castY;
public void Start()
{
blockType = 0;
}
void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 scaledPos = new Vector2 (Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
castX = (int)scaledPos.x;
castY = (int)scaledPos.y;
//Placing Block IDs
if (Input.GetMouseButtonDown(0))
{
if (!Physics.Raycast(ray, out hit, 20))
{
if (Input.mousePosition.x > 190)
{
switch (blockType)
{
case (0):
Debug.Log("No Block-Type Selected!");
break;
case (97):
Instantiate(id97, scaledPos, transform.rotation);
break;
case (214):
Instantiate(id214, scaledPos, transform.rotation);
break;
default:
blockType = 0;
Debug.Log("Unknown Error, Reverting to Block ID 0");
break;
}
}
} else
{
Debug.Log("Cannot place, already a block here!");
}
}
if (Input.GetKey(KeyCode.Z))
{
blockType = 97;
}
}
}
Using Unity v5.6.0f3 Personal
Answer by Gman0064 · Aug 27, 2017 at 11:19 AM
Figured out the problem. The package "SerializeHelper" by @Cherno causes some issues with GameObjects accessing other GameObjects in the scene after loading them in. The button was sending the correct Block ID number, but it never got received by the Level Editor script, and so nothing changed.
Rough and rugged fix was to combine the entire level editor and all the other stuff it needs into one big GameObject, save that prefab, and then spawn that in instead of multiple individual ones.