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 GarbagePile · Aug 06, 2016 at 07:50 PM · editorprefabvector3public variableguilayout.button

Public variables set with custom editor buttons reset to prefab values on play?

I am working on a 2D platformer and have a script for moving objects from one Vector2 position to another (simply titled "MovingObject"). I have 2 public Vector2 values for the 2 positions I want the objects to move between ("minPos" and "maxPos"). To make it easier to set the positions of these objects, I have created an editor script with custom buttons for setting these values.

The problem with this is that, when setting values using the custom buttons in objects placed from prefabs, the prefab connections of the values are not broken. As a result, the values reset to what they are defined as in the prefab (all 0) when I click play. The connections are broken when I edit each value manually, but having to do this with each object would make things far more tedious for me.

Here is my editor script:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(MovingObject))]
 public class MovingObjectEditor : Editor {
 
     public override void OnInspectorGUI(){
         DrawDefaultInspector();
 
         MovingObject mo = (MovingObject)target;
         
         if (GUILayout.Button ("Set Min Pos"))
             mo.SetMinPos ();
         if (GUILayout.Button ("Set Max Pos"))
             mo.SetMaxPos ();
         if (GUILayout.Button ("Move To Min"))
             mo.MoveToMin ();
     }
 }


These are the 3 functions referred to in the editor script:

     public void SetMinPos() {
         minPos = transform.position;
     }
     public void SetMaxPos() {
         maxPos = transform.position;
     }
     public void MoveToMin() {
         transform.position = minPos;
     }


I have looked at several solutions to this problem, but have not gotten any to work properly. Any help is appreciated.

Comment
Add comment · Show 2
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
avatar image bachirk · Sep 01, 2016 at 08:33 AM 0
Share

have you found any solutions? I'm having the same issue with Unity 5.3.6 This is definitely a Unity bug. Whatever values I set in the editor, get ignored and are reset to the prefab value the $$anonymous$$ute I hit Play.

avatar image GarbagePile bachirk · Sep 02, 2016 at 08:46 PM 0
Share

I have yet to find any definite solutions. The closest thing I've found is this, but I couldn't figure out how to get it to work with my scripts: http://answers.unity3d.com/questions/685602/using-a-custom-editor-script-with-a-prefab-instanc.html

I haven't had much time to work on this recently, but I may eventually replace this system altogether with something else that I think will better suit the project I'm working on. Still, it'd definitely be nice to know how to fix this in the future.

EDIT: So yeah, I ended up removing the "setting values with buttons" system and replaced it with a system where my object moves to another GameObject and back (this is easier to view in the scene). But as I said before, I'd still like to know how to fix this issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SwizzerSweet · Sep 08, 2016 at 03:06 AM

This was a common issue I was able to fix this issue by:

  1. Ensuring game is not running

  2. When modifying prefab, if your
    modifying the one in the scene, you need to drag it to your project
    assets prefab to update it

  3. Drag the prefab from your project to said script

Comment
Add comment · Show 1 · 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
avatar image GarbagePile · Sep 08, 2016 at 08:52 PM 0
Share

This updates the prefab's values. What I wanted is for each individual prefab instance to have separate values. This is definitely possible when setting the values for each individual object, but it doesn't work for me when using the buttons.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Set prefab to public variable of another script 1 Answer

Editor windows disappear on play in Unity 5.5.2f1 1 Answer

Why my prefab is auto changing? 3 Answers

How to prevent Unity editor from renaming public variables? 3 Answers

How can i convert a prefab instance to an ordinary game object? 2 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