I created a custom editor for the inspector of one of my scripts. The issue is, when I create a prefab of an object with the script attached, the prefab’s array will override the array of every instance to be the same.
What causes this and how can I prevent it?
EDIT: I am running unity version 5.6.2f1
Editor Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PointMovingPlatform))]
public class PointMovingPlatformEditor : Editor {
PointMovingPlatform pmp;
public void OnEnable()
{
pmp = (PointMovingPlatform)target;
}
public override void OnInspectorGUI()
{
///////////////////////////////////////
//Display speed
///////////////////////////////////////
pmp.speed = EditorGUILayout.FloatField("Speed", pmp.speed);
GUILayout.Space(20);
/////////////////////////////////////////
//Display waypoints array
// - Allow adding/removing of points
/////////////////////////////////////////
if (GUILayout.Button("Add Waypoint"))
{
AddWaypoint();
}
for (int i = 0; i < pmp.waypoints.Count; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Waypoint " + i.ToString());
GameObject old = pmp.waypoints*;*
pmp.waypoints = (GameObject)EditorGUILayout.ObjectField(pmp.waypoints*, typeof(GameObject), true);*
if(old != pmp.waypoints*)*
{
if(old.name == “Waypoint”)
DestroyImmediate(old);
}
if (GUILayout.Button(“[X]”))
{
RemoveWaypoint(i);
}
GUILayout.EndHorizontal();
}
//Display original for reference
GUILayout.Space(40);
GUILayout.Label(“ORIGINAL INSPECTOR”);
base.OnInspectorGUI();
}
void AddWaypoint()
{
GameObject newWP = new GameObject(“Waypoint”);
newWP.transform.position = new Vector3(pmp.platform.transform.position.x, pmp.platform.transform.position.y, pmp.platform.transform.position.z);
newWP.transform.parent = pmp.transform;
pmp.waypoints.Add(newWP);
}
void RemoveWaypoint(int index)
{
DestroyImmediate(pmp.waypoints[index].gameObject);
pmp.waypoints.RemoveAt(index);
}
}
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointMovingPlatform : MonoBehaviour {
public float speed;
public GameObject platform;
public List waypoints;
//public Transform[] waypoints;
private Vector3 direction;
private Transform destination;
private int currentWaypoint = 0;
* void Start () {*
* if(waypoints.Count > 0)*
{
destination = waypoints[currentWaypoint].transform;
direction = (destination.position - platform.transform.position).normalized;
}
* }*
void Update()
{
platform.transform.Translate(direction * speed * Time.deltaTime);
float distance = Vector3.Distance(platform.transform.position, destination.position);
if (distance < speed * Time.deltaTime || distance < speed / 20f)
{
platform.transform.position = destination.transform.position;
NextDestination();
}
}
private void OnDrawGizmos()
{
for(int i = 0; i < waypoints.Count; i++)
{
Gizmos.color = Color.grey;
Gizmos.DrawWireCube(waypoints*.transform.position, platform.transform.localScale);*
Gizmos.color = Color.black;
if(i + 1 >= waypoints.Count)
{
Gizmos.DrawLine(waypoints[waypoints.Count - 1].transform.position, waypoints[0].transform.position);
} else
{
Gizmos.DrawLine(waypoints*.transform.position, waypoints[i + 1].transform.position);*
}
if(waypoints.Count > 0)
{
Gizmos.DrawLine(platform.transform.position, waypoints[currentWaypoint].transform.position);
}
}
}
void NextDestination()
{
if (waypoints.Count > 0)
{
currentWaypoint++;
if(currentWaypoint >= waypoints.Count)
{
currentWaypoint = 0;
}
destination = waypoints[currentWaypoint].transform;
direction = (destination.position - platform.transform.position).normalized;
}
}
}
I apologize for the large amount of code, but I just want to ensure there is enough content to determine why this issue arises. Thank you in advance.
Your code worked fine for me. Maybe your instance of unity is damaged! Because this is highly irregular behavior. It shouldn't happen.
– MiladZarrin1