How to stop raycast activating trigger more than once.

Hi there, I have in a scene an object with the script below, I only want it to instantiate the object once but it happens repeatedly for as long as the player looks at it, any ideas? Thanks in advance for the input.

using System.Collections;
using UnityEngine;

public class RaycastSelect : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    Ray myRay;
    RaycastHit hit;
    public GameObject objectToInstantiate;
    public Transform Spawnpoint;

    // Update is called once per frame
    void Update()
    {
        myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
   
        if (Physics.Raycast (myRay, out hit))
        {
            Instantiate(objectToInstantiate, Spawnpoint.position, Spawnpoint.rotation);
        }
    }
}

After your line 24 starting with Instantiate, add line enabled = false;

1 Like

OMG thank you so much this worked perfectly!!

1 Like