Skip to main content
added 828 characters in body
Source Link
coder_86
  • 175
  • 1
  • 5
  • 19

In unity we can have different instances of a prefab with different properties. Unity represents the overridden property with bold characters. This works fine when manually change the value of prefab instance. But when I do it using a editor script, it do not turn bold and the property value changes to the source prefab when I play the game.

Here is an example of the code:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(testScript))]
public class editor : Editor {
    public override void OnInspectorGUI(){
        DrawDefaultInspector ();
        testScript scrt= (testScript)target;
        if (GUILayout.Button ("button")) {
            scrt.test();
        }
    }
}

.

using UnityEngine;
public class testScript: MonoBehaviour {
   test(){
       gameManager  =GameObject.Find("GameManager");
       gameManager.GetComponent<gameManager>().arr = new GameObject[3];
   }
}

.

using UnityEngine;
public class gameManger:MonoBehaviour{
    public GameObject[] arr;
}

The arr array in gamemanager class is not overridden and it changes to the source prefab value on play.

EDIT:

actual function called from editor script

    public void assignPortalPass(){
    GameObject gameManagerObj = GameObject.Find ("Game Manager");
    gameManager gameManagerScript = gameManagerObj.GetComponent<gameManager> (); 
    Transform passed = GameObject.Find ("passed").transform;
    int noOfChild = passed.childCount;
    if (noOfChild != gameManagerScript.noOfPortals) {
        Debug.LogError ("Check passed objects and noOfPortals (class gameManager) because both values are different");
        return;
    }
    Undo.RecordObject(gameManagerObj, "Made some changes to gameManager");
    //as suggested by the answer

    gameManagerScript.portalPass = new GameObject[noOfChild];
    for (int i = 0; i < noOfChild; i++) {
        gameManagerScript.portalPass [i] = passed.GetChild (i).gameObject;

    }
}

}

In unity we can have different instances of a prefab with different properties. Unity represents the overridden property with bold characters. This works fine when manually change the value of prefab instance. But when I do it using a editor script, it do not turn bold and the property value changes to the source prefab when I play the game.

Here is an example of the code:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(testScript))]
public class editor : Editor {
    public override void OnInspectorGUI(){
        DrawDefaultInspector ();
        testScript scrt= (testScript)target;
        if (GUILayout.Button ("button")) {
            scrt.test();
        }
    }
}

.

using UnityEngine;
public class testScript: MonoBehaviour {
   test(){
       gameManager  =GameObject.Find("GameManager");
       gameManager.GetComponent<gameManager>().arr = new GameObject[3];
   }
}

.

using UnityEngine;
public class gameManger:MonoBehaviour{
    public GameObject[] arr;
}

The arr array in gamemanager class is not overridden and it changes to the source prefab value on play.

In unity we can have different instances of a prefab with different properties. Unity represents the overridden property with bold characters. This works fine when manually change the value of prefab instance. But when I do it using a editor script, it do not turn bold and the property value changes to the source prefab when I play the game.

Here is an example of the code:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(testScript))]
public class editor : Editor {
    public override void OnInspectorGUI(){
        DrawDefaultInspector ();
        testScript scrt= (testScript)target;
        if (GUILayout.Button ("button")) {
            scrt.test();
        }
    }
}

.

using UnityEngine;
public class testScript: MonoBehaviour {
   test(){
       gameManager  =GameObject.Find("GameManager");
       gameManager.GetComponent<gameManager>().arr = new GameObject[3];
   }
}

.

using UnityEngine;
public class gameManger:MonoBehaviour{
    public GameObject[] arr;
}

The arr array in gamemanager class is not overridden and it changes to the source prefab value on play.

EDIT:

actual function called from editor script

    public void assignPortalPass(){
    GameObject gameManagerObj = GameObject.Find ("Game Manager");
    gameManager gameManagerScript = gameManagerObj.GetComponent<gameManager> (); 
    Transform passed = GameObject.Find ("passed").transform;
    int noOfChild = passed.childCount;
    if (noOfChild != gameManagerScript.noOfPortals) {
        Debug.LogError ("Check passed objects and noOfPortals (class gameManager) because both values are different");
        return;
    }
    Undo.RecordObject(gameManagerObj, "Made some changes to gameManager");
    //as suggested by the answer

    gameManagerScript.portalPass = new GameObject[noOfChild];
    for (int i = 0; i < noOfChild; i++) {
        gameManagerScript.portalPass [i] = passed.GetChild (i).gameObject;

    }
}

}

Source Link
coder_86
  • 175
  • 1
  • 5
  • 19

Prefabs property is not overriding when changing from editor script?

In unity we can have different instances of a prefab with different properties. Unity represents the overridden property with bold characters. This works fine when manually change the value of prefab instance. But when I do it using a editor script, it do not turn bold and the property value changes to the source prefab when I play the game.

Here is an example of the code:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(testScript))]
public class editor : Editor {
    public override void OnInspectorGUI(){
        DrawDefaultInspector ();
        testScript scrt= (testScript)target;
        if (GUILayout.Button ("button")) {
            scrt.test();
        }
    }
}

.

using UnityEngine;
public class testScript: MonoBehaviour {
   test(){
       gameManager  =GameObject.Find("GameManager");
       gameManager.GetComponent<gameManager>().arr = new GameObject[3];
   }
}

.

using UnityEngine;
public class gameManger:MonoBehaviour{
    public GameObject[] arr;
}

The arr array in gamemanager class is not overridden and it changes to the source prefab value on play.