Answer the question
In order to leave comments, you need to log in
How to create an object in the scene from the Inspector?
(Unity 5.3, C#)
I need:
I create a script -> hang it on an object -> change the values of the variables of this script through the inspector (for example, the number of objects created) and immediately the objects are created in the scene, before the game itself starts .
I don't know if this is even possible in Unity.
So far I'm using Awake, specifying values through the inspector. But this is too inconvenient, because. constantly need to run the game to check.
I searched on the Internet - I did not find it. Maybe I just didn't search well.
Answer the question
In order to leave comments, you need to log in
There are several options. If you want to use the same method in the editor and runtime, then you can make your own inspector with a button. For example, you want to call the Method method in the editor:
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
Method();
}
public void Method()
{
Debug.Log("Method");
}
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof (Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Method"))
{
((Test) target).Method();
}
}
}
using UnityEditor;
using UnityEngine;
public static class TestUtility
{
[MenuItem("Tools/Method")]
public static void Method()
{
Debug.Log("Method");
}
}
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
[HideInInspector, SerializeField]
private List<GameObject> gameObjects = new List<GameObject>();
private void Awake()
{
Generate();
}
public void Generate()
{
for (int i = 0; i < 100; i++)
{
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
gameObjects.Add(go);
}
}
public void Clear()
{
foreach (var go in gameObjects)
{
if (Application.isPlaying)
{
Destroy(go);
}
else
{
DestroyImmediate(go);
}
}
gameObjects.Clear();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question