After one instantiate by on click event, cannot instantiate again

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

public class QuestionManagement : MonoBehaviour
{
    Vector3 startPosition;
    Vector3 targetPosition;
    public float moveSpeed = 3f;
    private bool isMoving = false;
    private GameObject targetObject;
    private bool nodeCreated = false;
    public GameObject myPrefab;

    void Update()
    {
        if (isMoving)
        {
            targetObject.transform.position = Vector3.Lerp(targetObject.transform.position, targetPosition, moveSpeed * Time.deltaTime);
            if (Vector3.Distance(targetObject.transform.position, targetPosition) < 0.1f)
            {
                targetObject.transform.position = targetPosition;
                isMoving = false;
                killGameObject(targetObject);
                nodeCreated = false;
                if (nodeCreated == false)
                {
                    StartCoroutine(DelayCoroutine());
                }
            }
        }
    }

    public void moveObject(GameObject gameObject)
    {
        startPosition = gameObject.transform.position;
        targetPosition = new Vector3(startPosition.x - 800f, startPosition.y, startPosition.z);
        targetObject = gameObject;
        isMoving = true;
    }

    IEnumerator DelayCoroutine()
    {
        yield return new WaitForSeconds(1f);
        createNode(myPrefab);
    }

    public void createNode(GameObject gameObject)
    {
        GameObject newNode = Instantiate(myPrefab, new Vector3(startPosition.x, startPosition.y, startPosition.z), Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
        nodeCreated = true;

        LogChildObjects(newNode.transform);

        Button acceptButton = transform.Find("QuestionNode(Clone)/QuestionText/AcceptButton")?.GetComponent<Button>();
        Button denyButton = transform.Find("QuestionNode(Clone)/QuestionText/DenyButton")?.GetComponent<Button>();

        if (acceptButton != null)
        {
            acceptButton.onClick.AddListener(() => AcceptAction(newNode));
            //Debug.Log("AcceptButton found and event added.");
        }
        else
        {
            // Debug.LogError("AcceptButton not found in the instantiated prefab.");
        }

        if (denyButton != null)
        {
            denyButton.onClick.AddListener(() => DenyAction());
            //Debug.Log("DenyButton found and event added.");
        }
        else
        {
            //Debug.LogError("DenyButton not found in the instantiated prefab.");
        }
    }

    void LogChildObjects(Transform parent)
    {
        foreach (Transform child in parent)
        {
            LogChildObjects(child);
        }
    }

    public void killGameObject(GameObject gameObject)
    {
        Destroy(gameObject, 2);
    }

    public void AcceptAction(GameObject gameObject)
    {
        Debug.Log("Accept button clicked.");
        moveObject(gameObject);
        StartCoroutine(DelayCoroutine());
    }

    public void DenyAction()
    {
        Debug.Log("Deny button clicked.");
    }
}

I will take a guess that it is at least partially related to lines 56 and 57. Your transform.Find isn’t finding on the instantiated object.

Hi !

First of all, thanks for answering.

I thought that but after one click prefab created and after that I can use one more time on created prefab but it doesn’t work after third attempt. So it is little confused me.

That’s just means you wrote a bug, and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

ALSO:

Keep in mind that using ANY kind of “Find” or GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

Here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:

  • what you’re looking for:
    → one particular thing?
    → many things?
  • where it might be located (what GameObject?)
  • where the Get/Find command will look:
    → on one GameObject? Which one? Do you have a reference to it?
    → on every GameObject?
    → on a subset of GameObjects?
  • what criteria must be met for something to be found (enabled, named, etc.)
  • if your code expects one instance and later you have many (intentional or accidental), does it handle it?

If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.

This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.

If you run into an issue with any of these calls, start with the documentation to understand why.

There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.

Thanks a lot for advices. I figured it out anyways, it was timing issue between creating moving and destroying game object.