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?

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.