Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 11, 2019 at 01:19 PM by kaliAJ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kaliAJ · Sep 22, 2019 at 01:17 PM · variablearray of gameobjectsincrementcannot

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);
     }
 }
 

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaliAJ · Sep 22, 2019 at 01:22 PM 0
Share
 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);
     }
 }
 

 


avatar image kaliAJ · Sep 22, 2019 at 01:26 PM 0
Share

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!!!!

avatar image Santifocus kaliAJ · Sep 22, 2019 at 01:40 PM 0
Share

Im sorry but could you share how exactly you are using the bulletCount variable? because based on this information everything should work.

avatar image kaliAJ Santifocus · Sep 23, 2019 at 06:19 AM 0
Share

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"

Show more comments

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaliAJ · Sep 23, 2019 at 06:24 AM 0
Share

@$$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.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

On Click, increment variable 1 Answer

How to make a variable fall/rise over time? 1 Answer

variable increments multiple times ! 0 Answers

What is Wrong With The Logic in My Variable? 2 Answers

How can I use the same script on multiple objects without conflicting variables. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges