In my script I’m trying to instantiate an object, but only once. Yes, the function call is in the update function however I have a variable that should stop it from being instantiated again. Please take a look at the relevant code and see if you can find a problem.
*EDIT: I have included the complete set of code, as the problem has not yet been solved with the previous amount information.
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;
Debug.Log("Mine Button Pressed");
}
}
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;
Debug.Log("Timer Started");
if(_timer >= cycleTime) {
Debug.Log("Cycle Completed");
oreMined = Mathf.RoundToInt(m3PerSec * cycleTime);
target.GetComponent<Asteroid>().oreAmount -= oreMined;
GameObject.Find("PlayerCargo").GetComponent<PlayerCargo>().AddCargo(_mineProductGameObject, oreMined);
_timer = 0;
_mine = true;
Debug.Log("Start Cycle Again");
if(target.GetComponent<Asteroid>().oreAmount <= oreMined)
Destroy(target);
}
}
}
And here is the code for the beam:
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);
}
}
On what object is this attached to? If its on the instantiated object, move it to something that there is only one occurrence of. Also try placing a Debug.Log right before you instantiate, should give you some clues of why its done multiple times.
– LinusLooking at the code, I see nothing that would cause it to call itself twice. I guess you would need to post what calls
– RudyTheDevMine().@Linus: It is not on the projectile, it's attached to the launcher. @RudyTheDev: Thats why I decided to go to the answers :p The instantiate is actually being called each frame, not just twice. Heres the call code if you want to take a look. void OnGUI() { if(GUI.Button(new Rect(10, 10, 50, 50), "Mine")) { _mine = true; } } void Update() { if(_mine == true) { Mine(); } } I know it's not efficient but I'm trying to get something that works right before I optimize it.
– Benifirvoid Update() { if(_mine == true) { Mine(); _mine = false; } }
– Linus