How to Instantiate Object only Once?

In my script I’m trying to instantiate an object, but only once as the title states. Yes, the function call is in the update function however I have a variable that should stop it from being instantiated over and over again. I’ve added many debug logs into the shooter scrip and after the line

    go.GetComponent<MiningLaserBeam>().muzzle = muzzle;
	Debug.Log ("Muzzle Set");

none of the debug lines will show up in the inspector (The code stops there then restarts from the top?). Also it appears an object is being instantiated not once but every frame.

Please take a look at the code and see if you can find a problem.

Here is the script for the mining laser turret:

using UnityEngine;
using System.Collections;

public enum MiningLaserClass {
	Class1,
	Class2
}

public class MiningLazer : MonoBehaviour {

	#region Mining Public Variables
	public MiningLaserClass laserClass;

	public GameObject target;

	public float cycleTime = 60;
	public float m3PerSec = 0.35f;

	public float range = 10.6f;

	public int oreMined;

	public GameObject beamPrefab;
	#endregion

	#region Aiming Public Variables
	public GameObject muzzle;
	public GameObject barrel;

	public float rotateRate = 50;

	public AudioClip fireSound;
	public AudioClip rotateSound;
	#endregion

	#region Private Variables
	private float _elapsedOreMined;
	private float _timer;
	private bool _mine;
	private bool _instantiated = false;
	
	private string _mineProduct;
	private GameObject _mineProductGameObject;
	#endregion
	
	void OnGUI() {
		if(GUI.Button(new Rect(10, 10, 50, 50), "Mine")) {
			_mine = true;
		}
	}

	void Update() {
		target = GameObject.FindGameObjectWithTag("Player").GetComponent<WeaponManager>().target;

		if(_mine == true && target != null) {
			Mine();
			_mine = false;
		}

		if(target != null) 
			AimAtTarget();
	}

	public void Mine() {
		Debug.Log("Started Mining");

		target.rigidbody.constraints = RigidbodyConstraints.FreezePosition;

		_mineProduct = target.GetComponent<Asteroid>().oreType;
		_mineProductGameObject = GameObject.Find(_mineProduct);

		if(_instantiated == false) {
			InstantiateBeam();
		}

		StartTimer();
	}

	public void InstantiateBeam() {
		GameObject go = GameObject.Instantiate(beamPrefab, muzzle.transform.position, muzzle.transform.rotation) as GameObject;
		go.GetComponent<MiningLaserBeam>().muzzle = muzzle;
		go.GetComponent<MiningLaserBeam>().DisplayBeam();
		_instantiated = true;
	}

	public void AimAtTarget() {
		Vector3 lookPos = target.transform.position - transform.position;
		
		Quaternion rotation = Quaternion.LookRotation(lookPos, transform.up);
		
		Vector3 targetAngles = rotation.eulerAngles;
		
		Vector3 currentAngles =	transform.rotation.eulerAngles;
		Vector3 barrelCurrentAngles = barrel.transform.rotation.eulerAngles;
		
		currentAngles.y = Mathf.LerpAngle(currentAngles.y, targetAngles.y,Time.deltaTime * rotateRate);
		barrelCurrentAngles.x = Mathf.LerpAngle(barrelCurrentAngles.x, targetAngles.x, Time.deltaTime * rotateRate);
		barrelCurrentAngles.y = currentAngles.y;
		barrelCurrentAngles.z = currentAngles.z;
		
		transform.eulerAngles = currentAngles;
		barrel.transform.eulerAngles = barrelCurrentAngles;
	}

	public void StartTimer() {
		_timer += Time.deltaTime;

		if(_timer >= cycleTime) {
			oreMined = Mathf.RoundToInt(m3PerSec * cycleTime);
			target.GetComponent<Asteroid>().oreAmount -= oreMined;
			GameObject.Find("PlayerCargo").GetComponent<PlayerCargo>().AddCargo(_mineProductGameObject, oreMined);
			_timer = 0;
			_mine = true;

			if(target.GetComponent<Asteroid>().oreAmount <= oreMined)
				Destroy(target);
		}
	}
}

And the laser beam if neccesary:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(LineRenderer))]
public class MiningLaserBeam : MonoBehaviour {
	public float range;
	public LineRenderer beam;
	public Material beamMaterial;
	
	public GameObject muzzle;

	private GameObject _target;


	void Start () {
		beam = GetComponent<LineRenderer>();
		beam.SetVertexCount(2);
		beam.renderer.material = beamMaterial;
		beam.SetWidth(0.2f, 0.2f);
	}
	
	void Update () {
		_target = GameObject.FindGameObjectWithTag("Player").GetComponent<WeaponManager>().target;

		DisplayBeam();
	}

	public void DisplayBeam() {
		beam.enabled = true;
		beam.SetPosition(0, muzzle.transform.position);
		beam.SetPosition(1, _target.transform.position);
	}
}

As a note, you probably don’t want to use GameObject.Find or FindWithTag since it’s pretty heavy to use. If you’re testing it’s fine but wanted to note that for down the line you should try to avoid that and change those to be set other ways.

Also from how I read it, it seems like your timer will only trigger once. It isn’t repeating or anything so it will just set timer to be timer+= deltaTime, and either pass or fail the condition, if it fails you won’t ever reset _mine to true. That makes it more odd that it’s instantiating every frame.

Is Mine() being called every frame as well? Are you getting it’s debug every frame?

If it is, try switching the order of Mine(); and _mine = false; so:

  if(_mine == true && target != null) {
     _mine = false;         
     Mine();     
   }

I’ve had it before where the condition would get checked more than once before the condition was reset. I don’t think that should be the issue, but it may be part of it.