- Home /
How to run script in editor
I want to make a script for editing a scene to make it easier to place down prefabs instead of duplicating and moving prefabs hundreds of times.
Here's my script:
using UnityEngine;
using System.Collections;
public class LevelEditingScript : MonoBehaviour {
void Update () {
if (Input.GetKey(KeyCode.Space) && Input.GetMouseButtonDown(0)) {
Debug.Log("mouseclick");
GameObject objectPrefab = Instantiate (UnityEditor.Selection.activeObject) as GameObject;
objectPrefab.transform.position = Camera.main.ScreenToWorldPoint (Input.mousePosition);
objectPrefab.transform.position = new Vector3 (objectPrefab.transform.position.x, 0.1f, objectPrefab.transform.position.z);
objectPrefab.transform.SetParent (GameObject.Find ("Obstacles").transform);
}
}
}
This script instantiates the selected gameobject in the inspector when the space bar and left mouse button is clicked. I would instantiate things like trees, rocks and flowers.
Is there any way to run this script in the editor and not play mode?
Answer by N1warhead · Oct 22, 2016 at 07:50 PM
What @Raresh said does in fact work. The problem you may be facing is that the editor doesn't support Input.Getkey stuff. (Not sure if that's the case for an in-game) running script with ExecuteInEditMode. But you'll very easily know if it does work.
Put a Debug.Log at the top of your update method. if it shows the log, then it's in fact the KeyCode and Mouse issues.
If that's the case, then learn the Event System.
But truthfully, you're better off using a script like this as an Editor Script. Which is more suited for stuff like this if you want to do it in the actual Editor.... Yes I know it's quite a bit different with how some things must be done. But you won't regret it.
Not trying to advertise, but Eclectic $$anonymous$$aster System I just put on the asset store can handle things like this for you. Just figured I'd point that out for ya if you can't seem to get this working.
Your answer
Follow this Question
Related Questions
What is runtime analogy for Editor's PrefabUtility.SetPropertyModifications? 0 Answers
changing a bool in a prefab doesn't change in it's instance. 2 Answers
Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers
Saving a spheremodel into a prefab 1 Answer
When switching scenes some of my scripts stop working 0 Answers