Unity3D - RTS Game - Instantiate object on buttonclick and make it follow the cursors position

I am trying to instantiate an object of my “ObjectManager”-class by clicking on a button and then to make it follow my cursors position. Would like to know what I am doing wrong in this following code.
I appreciate your support.


So, when the selectedObj is selected I want it to move it along my cursors positon. It is working great but expect of instantiated objects. Part of my SelectionManager script:

 void Update()
    {
        Ray currentCursorPos = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hitInfo;

        if (Physics.Raycast(currentCursorPos, out hitInfo, ignoreLayer))
        {
            objThatIsHit = hitInfo.collider.GetComponent<ObjectManager>();

            if (selectedObj == null || selectedObj.inSelection == false)
            {
                selectedObj = (objThatIsHit.gameObject.tag == "obstacle" || objThatIsHit.gameObject.tag == "field") ? objThatIsHit : selectedObj;

                OnSelect(); // Turns "inSelection" in true when Input.GetMouseButtonDown(0) == true
            }
            else if (selectedObj != null)
            {
                selectedObj.transform.position = (selectedObj != objThatIsHit) ? hitInfo.point : selectedObj.transform.position;

                OnDeselect(); // Turns "inSelection" in false when Input.GetMouseButtonDown(0 or 1) == true
            }
        }
    }

Below you can find the “objToSelect” variable which is temporarily made to obtain the instantiated object. After that I push this object to my “selectedObj” of my SelectionManager script and turn it’s “inSelection” in true so it follows my cursor. But it’s not working. The instantiated object just stops on a position next to the button I pressed but it’s not moving until I click on it.

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

public class UnitMenu : MonoBehaviour
{
    [SerializeField] private GameObject[] units = null;
    [SerializeField] private Texture2D[] unitIcon = null;

    [SerializeField] private GameObject ghost3dTemplate;

    private SelectionManager selectionManager;
    private Vector3 objThatIsHit;

    private void Start()
    {
        selectionManager = GameObject.Find("SelectionManager").GetComponent<SelectionManager>();
    }


    void OnGUI()
    {
        CreateUnitUI();
    }

    private void CreateUnitUI()
    {
        int offset = 165;

        GUI.Box(new Rect(Screen.width / 2 - 250, Screen.height - 125, 500, 100), "");

        for (int i = 0; i < units.Length; i++)
        {
            GUIStyle icon = new GUIStyle();
            icon.normal.background = unitIcon*;*

if (GUI.Button(new Rect(Screen.width / 2 - 245 + (offset * i), Screen.height - 120, 160, 90), units*.name, icon) && CheckSelection())*
{
// Here is my issue with the function I want - First I instantiate the object, then give it to selectedObj and turn it’s inSelection value in true
GameObject objToSelect = Instantiate(ghost3dTemplate, new Vector3(0,1,0), Quaternion.identity) as GameObject;
selectionManager.selectedObj = objToSelect.GetComponent();
selectionManager.selectedObj.inSelection = true;
}
}
}
// Is checking if an object is already selected - if an object is selected the buttons aren’t possible to be pressed
private bool CheckSelection()
{
if (selectionManager.selectedObj == null || selectionManager.selectedObj.inSelection == false)
{
return true;
}

return false;
}
}

is the OnDeselect triggered when you instantiate the object?

Solved it by myself. I use an ObjectManager script to get informations about my objects. I told the game to give each object when it’s instantiated to be not in selection. It wasn’t necessary so I removed it and it’s working great. Sometimes it is really funny to see what a small code snippet can do with your game :smiley:


private void Start()
    {
        currentMaterial = GetComponent<Renderer>().material;
        originMaterial = currentMaterial;

        inSelection = false; // This wasn't necessary and it made the problem
    }

EDIT: I changed something to avoid issues when instantiating objects. I changed in the ObjectManager script the “Start()” function to “Awake()”.