Should I remove this bug?

Updated on March 25, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on March 22, 2017

Hey guys might sound like a silly question but I am making a clone of the game super crate box. In a summary player collects box picks up a gun but destroys old gun before picking up another. I am using a debug.log to check how many there are currently in the guns <list> variable most times says 1 but then sometimes says 2 there is a quick pop up in the inspector I believe and it shows 2 guns, I think its because the OnTrigger mixed with the update function. But inspector thing only pops up for a split second doesn’t affect the game.

this is my code:


public class PickupBx : MonoBehaviour

{

//Spawn locations for boxes

public Vector2[] box_locations;
//this gets the prefab

public GameObject boxPrefab;
public GameObject player;
public GameObject[] gunPrefab;
//this stores the current gameobject which is in the game

public GameObject activatedGameObject;
//keep count of times weapon collected

private int count = 0;
//Randomly generated number for weapons.

private int randomWeaponNumber;
//holds the randomNumber

private int random_number;
//holds the gun objects

public List<GameObject> guns = new List<GameObject>();
//checks if weapon is collected

private bool isWeaponCollected = false;
// Use this for initialization

void Start()

{

//Generate random number between 0-3 there are 4 vector2 locations for the box.

random_number = Random.Range(0, 3);
//Instantiate the prefab at a random location and set its rotation to default

activatedGameObject = (GameObject)Instantiate(boxPrefab, box_locations[random_number], Quaternion.identity);
//if tag on box object is not box then set it to box (already configured tag, this is here for extra security)

if (boxPrefab.tag != "box")

boxPrefab.tag = "box";

}
//keep generating random numbers through Update

void Update()

{

//this will store the game object that has been found

GameObject activeGunObject;
//this part will need fixing later

if (this.gameObject.GetComponent<SpriteRenderer>().flipX == true && GameObject.FindGameObjectWithTag("Gun") != null)

{

//activate game object is equal to the gun found in the game

activeGunObject = GameObject.FindGameObjectWithTag("Gun");

//flip gun to x axis

activeGunObject.GetComponent<SpriteRenderer>().flipX = true;

//this is the position of the gun when x axis is true

activeGunObject.transform.localPosition = new Vector2(-44.1f, 0);
}
else if (this.gameObject.GetComponent<SpriteRenderer>().flipX == false && GameObject.FindGameObjectWithTag("Gun") != null)

{

activeGunObject = GameObject.FindGameObjectWithTag("Gun");

activeGunObject.GetComponent<SpriteRenderer>().flipX = false;

activeGunObject.transform.localPosition = new Vector2(44.1f, 0);

}
//random Number for box location

random_number = Random.Range(0, 3);
//random Number for random gun

randomWeaponNumber = Random.Range(0, 3);
// guns in inventory/list greater than or equal to 2 delete the gun

if (guns.Count >= 2)

{

//we destroy the gun otherwise there will be multiple guns in the scene (reaons is guns.count - guns.count is because some weird glitch if we do guns.count -1 count enters twice

guns.RemoveAt(guns.Count - guns.Count);
//put back to zero

count = 0;
//is true because weapon collected

isWeaponCollected = true;

}
//Call the destroy weapon function

DestroyWeapon();

}
void OnTriggerEnter2D(Collider2D col)

{

if (col.gameObject.tag == "box")

{

//add one to count

count += 1;
Debug.Log(count + "count");

Debug.Log(guns.Count + "how many guns are there?");
//take box to random location

activatedGameObject.transform.position = box_locations[random_number];
//spawn random gun

GameObject currentGun = Instantiate(gunPrefab[randomWeaponNumber]);
//the transform of the player

Transform cameraTransform = player.transform;
//Makes the player the parent of the GameObject currentGun

currentGun.transform.SetParent(cameraTransform, false);
//add gun to list

guns.Add(currentGun);
count = 0;

}

}
//this weapon will destroy the weapon so there is one 1 in the scene

void DestroyWeapon()

{

if (isWeaponCollected == true)

{

//Destroy the gameobject with the gun tag so we don't make multiple guns

Destroy(GameObject.FindGameObjectWithTag("Gun"));
//set to false so update doesn't call this method.

isWeaponCollected = false;

}

}
}
  • Liked by
Reply
3 on March 25, 2017

bump

Devoted
on March 25, 2017

If it ain’t broke, don’t fix.

Devoted
on March 25, 2017

Unless you see it affecting functionality in the future don’t bother.
Instead, dedicate your time and resources to more pressing issues.

Wise
on March 25, 2017

Ok thanks for advice.

Show more replies
  • Liked by
Reply
Cancel