Hi all, i’m working on my first editor script which finds objects with a tag and then spawns an object in its place.
The bug I have now is
ReplacementItem = EditorGUILayout.ObjectField(ReplacementItem,GameObject);
outputs “Assets/TileSwap.cs(28,80): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected”
I have a few other problems with this code , like
Instantiate(ReplacementItem, replaceTile.transform.position, replaceTile.transform.rotation) as GameObject;
isn’t working for some reason, but I’d be greatful if someone could help me with the first problem …
using UnityEngine;
using System.Collections;
using UnityEditor ;
//[ExecuteInEditMode]
//[CustomEditor(TileSwap)]
public class TileSwap : EditorWindow{
public string SwapTag ;
public GameObject ReplacementItem ;
public GameObject[] replaceTiles;
//public GameObject[] replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
public GameObject Test ;
[MenuItem("Window/TileSwaper")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(TileSwap));
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
ReplacementItem = EditorGUILayout.ObjectField(ReplacementItem,GameObject);
// outputs "Assets/TileSwap.cs(28,80): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected"
EditorGUILayout.EndHorizontal();
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
SwapTag = EditorGUILayout.TextField ("Object Tag", SwapTag);
// DrawDefaultInspector(); //if you are just adding a button
//Debug.Log
if(GUILayout.Button ("Swap Tile")){
replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
foreach (GameObject replaceTile in replaceTiles) {
Debug.Log(replaceTile.name);
// Instantiate(ReplacementItem, replaceTile.transform.position, replaceTile.transform.rotation) as GameObject;
//Instantiate(ReplacementItem,Test.transform.position, Test.transform.rotation) as GameObject;
}
//do what you need to here when the button is pressed
}
}
}
Fixed it myself
using UnityEngine;
using System.Collections;
using UnityEditor ;
//[ExecuteInEditMode]
//[CustomEditor(TileSwap)]
public class TileSwap : EditorWindow{
public string SwapTag ;
public Object ReplacementItem ;
public GameObject[] replaceTiles;
//public GameObject[] replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
public GameObject Test ;
public TileSwap _target;
void OnEnable()
{
_target = (TileSwap)_target;
}
[MenuItem("Window/TileSwaper")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(TileSwap));
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
//GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");
//ReplacementItem = EditorGUILayout.ObjectField(ReplacementItem,GameObject);
ReplacementItem = EditorGUILayout.ObjectField("New Tile", ReplacementItem, typeof(UnityEngine.Object));
// outputs "Assets/TileSwap.cs(28,80): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected"
SwapTag = EditorGUILayout.TextField ("Object Tag", SwapTag);
EditorGUILayout.EndHorizontal();
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
//SwapTag ="SwapMe";
// DrawDefaultInspector(); //if you are just adding a button
//Debug.Log
if(GUILayout.Button ("Swap Tile")){
GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");
replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
foreach (GameObject replaceTile in replaceTiles) {
Debug.Log(replaceTile.name);
Instantiate(ReplacementItem, replaceTile.transform.position, replaceTile.transform.rotation);
//Instantiate(ReplacementItem,Test.transform.position, Test.transform.rotation) as GameObject;
}
//do what you need to here when the button is pressed
}
}
}
Final version for anyone who wants to use it
Gonna post this to the Wiki latter , has a delete replaced item option too .
Enjoy .
using UnityEngine;
using System.Collections;
using UnityEditor ;
//[ExecuteInEditMode]
//[CustomEditor(TileSwap)]
public class TileSwap : EditorWindow{
public string SwapTag ;
public Object ReplacementItem ;
public GameObject[] replaceTiles;
public bool myBool ;
//public GameObject[] replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
public GameObject Test ;
public TileSwap _target;
void OnEnable()
{
_target = (TileSwap)_target;
}
[MenuItem("Window/TileSwaper")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(TileSwap));
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
//GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");
//ReplacementItem = EditorGUILayout.ObjectField(ReplacementItem,GameObject);
ReplacementItem = EditorGUILayout.ObjectField("New Tile", ReplacementItem, typeof(UnityEngine.Object));
// outputs "Assets/TileSwap.cs(28,80): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected"
SwapTag = EditorGUILayout.TextField ("Object Tag", SwapTag);
myBool = EditorGUILayout.Toggle ("Delete Replaced Items", myBool);
EditorGUILayout.EndHorizontal();
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
// myBool = EditorGUILayout.Toggle ("Delete Replaced Items", myBool);
//SwapTag ="SwapMe";
// DrawDefaultInspector(); //if you are just adding a button
//Debug.Log
if(GUILayout.Button ("Swap Tile")){
GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");
replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
foreach (GameObject replaceTile in replaceTiles) {
Debug.Log(replaceTile.name);
Instantiate(ReplacementItem, replaceTile.transform.position, replaceTile.transform.rotation);
if(myBool)
{
DestroyImmediate(replaceTile);
// Destroy(replaceTile);
}
//Instantiate(ReplacementItem,Test.transform.position, Test.transform.rotation) as GameObject;
}
//do what you need to here when the button is pressed
}
}
}