- Home /
The question is answered, right answer was accepted
unable to increment the variable sitting on a gameobject from array of gameobjects
public class LightBullet : MonoBehaviour
{
public GameObject particleEffect;
public GameObject MultiBulletButton;
public GameObject Crosshair;
public GameObject Turret;
public int bulletCount = 0;
public void OnTriggerEnter(Collider other)
{
if(other.CompareTag("cubies"))
{
Pickup(other);
}
}
public void Pickup(Collider Player) // this function is working correctly but...
{
Destroy(Instantiate(particleEffect, transform.position, transform.rotation), 5f);
bulletCount++; // this variable doesn't get incremented. when the gameobject (on which this script is sitting) is spawned through the "powerup" array present inside the "Target" class. (script is uploaded below)
GetComponent<MeshRenderer>().enabled = false;
GetComponent<BoxCollider>().enabled = false;
if (Turret.activeInHierarchy)
{
Crosshair.SetActive(true);
}
FindObjectOfType<AudioManager>().Play("PowerUP");
MultiBulletButton.SetActive(true);
gameObject.SetActive(false);
}
}
public class Target : $$anonymous$$onoBehaviour
{
public int maxHealth = 100;
public GameObject cannonImpactEffect;
public GameObject crossHair;
public GameObject[] powerup;
public Vector3 center;
public Vector3 size;
public Vector3 Spawnpos;
private void Start()
{
InvokeRepeating("Spawn", 30f, 30f);
}
public void Spawn()
{
for (int i = 1; i <= powerup.Length / 5; i++)
{
Spawnpos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), -1.5f, Random.Range(-size.z / 2, size.z / 2));
GameObject toSet = Instantiate(powerup[Random.Range(0, powerup.Length)], Spawnpos, transform.rotation);
toSet.SetActive(true);
}
}
public void TakeDamage(int amount)
{
maxHealth -= amount;
if(maxHealth <= 0)
{
burst();
}
}
public void burst()
{
Instantiate(cannonImpactEffect, transform.position, transform.rotation);
gameObject.SetActive(false);
crossHair.SetActive(false);
for (int i = 0; i < powerup.Length / 2; i++)
{
Spawnpos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), -1.5f, Random.Range(-size.z / 2, size.z / 2));
GameObject toSet = Instantiate(powerup[Random.Range(0, powerup.Length)], Spawnpos, transform.rotation);
toSet.SetActive(true);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.DrawCube(center, size);
}
}
when i test the game by making those gameobjects active in heirarchy they works correctly.but when the same are spawned through the array the "bulletCount" variable dosen't increment. please helpppp!!!!
Im sorry but could you share how exactly you are using the bulletCount variable? because based on this information everything should work.
Basically "LightBullet" is the gameobject which is a (ammo) powerup when my player (cubies) collides with it a single bullet should be awarded to the player... Which is specified in the "pickup" function under class "LightBullet"
Answer by MlleBun · Sep 22, 2019 at 05:37 PM
You should use onTriggerStay instead (or in addition, if you need to). onTriggerEnter triggers on the fixed frame your colliders first interacted. onTriggerStay will update your desired variable when the colliders are still in contact.
@$$anonymous$$lleBun But when tested by making it active in heirarchy it worked absolutely fine. Only caused problem when I used the gameobject [] array. Which randomly select the power up from array and spawn them at random position.