Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gman0064 · Aug 27, 2017 at 07:59 AM · buttonserializationloadingsavingdatatype

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I am getting a serialization exception error when trying to save and load in Unity? 2 Answers

How to deserialize after I've added more members to a class 0 Answers

Issue loading serialized config.dat from persistant Data 0 Answers

Serializing / deserializing large lists 0 Answers

Serialized Data Not Saving on Android Build 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges