- Home /
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.
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.
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.
Answer by SwizzerSweet · Sep 08, 2016 at 03:06 AM
This was a common issue I was able to fix this issue by:
Ensuring game is not running
When modifying prefab, if your
modifying the one in the scene, you need to drag it to your project
assets prefab to update itDrag the prefab from your project to said script
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
Follow this Question
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