How can i use a button in the inspector to generate new map ? And how to change vars in real time ?

In this script i create a kind of grid map. But i have some problems.

First it’s not filling the grid. It’s doing only creating the outer width and height and only on two sides. But i want it to be filled like a grid map.

Another problem is that if i put as width and height 4 it should create 4X4 = 16 width height grid. But it’s creating over then 100 objects.

Also how can i make that if i will change the variables values in the editor it will effect the objects in real time ?

Last problem is how can i use the custom button ot generate each time a new grid ? Like reset.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class LevelMap : MonoBehaviour
{
    public GameObject Node;
    public Vector3 nodeSize;
    public int mapWidth;
    public int mapHeight;
    public float Spacing = 2.0f;

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < mapWidth; i++)
        {
            Instantiate(Node);
            float positionWidth = Spacing * (float)i;
            Node.transform.localPosition = new Vector3(positionWidth, 0, 0);

            for (int x = 0; x < mapHeight; x++)
            {
                Instantiate(Node);
                float positionHeight = Spacing * (float)x;
                Node.transform.localPosition = new Vector3(0, 0, positionHeight);
            }
        }
    }

    public void GenerateNew()
    {

    }
}

And the button script:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(LevelMap))]
public class customButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        LevelMap lm = (LevelMap)target;
        lm.GenerateNew();
    }
}

It does that. Just change any public (or [SerializeField]) in the Inspector and it changes it during play, you dont have to do anything special.

There are many ways:

  • Create a UI
  • Use the legacy UI (OnGUI)
  • Use the ContextMenu attribute - Video ← I would probably use this one

i have an issue what i ran to… why would you instantiate in an update function.
and above what TaleOf4Gamers said you could do that.

but there is also a way to code when you click the button. make sure that you attach the script onto the button it self.

gameObject.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => GenerateNew());

but i also improved your spawning so it does not spawn over 100+ objects.
you can call Start as an void but also as an IEnumerator is more effecient for your cpu to handle.

// Update is called once per frame
    IEnumerator Start()
    {
        for (int i = 0; i < mapWidth; i++)
        {
            for(int x = 0; x < mapHeight; x++)
            {
                GameObject go = Instantiate(Node, this.transform, true);

                float offsetWidth = Spacing * i;
                float offsetHeight = Spacing * x;

                go.transform.localPosition = new Vector3(offsetWidth, 0, offsetHeight);
            }
        }

        yield return null;
    }