- Home /
Running an instantiation method from bools and a left-click...
I am working on a project, where there will be nine weapons. These weapons will be activated once the kill count has reached a specific number. For this, I use bools for each weapon, and when the kill count reaches a specific number, the current weapon is set to false and the next is set to true. I then used "if" statements for each, so as to have unity instantiate a certain defined particle effect when the left click is held down. All that was logically possible, until I figured out that instead of one being instantiated, no matter which bool is marked "true", all the nine particles are instantiated upon left-clicking. How did this occur? Can this be repaired?
Thanks in advance for your support!
MY CODE:
AllWeaponSystem.cs
 using UnityEngine;
 using System.Collections;
 
 public class AllWeaponSystem : MonoBehaviour {
     
     public GameObject Worm;
     public GameObject Dazzler;
     public GameObject Disaster;
     public GameObject Grief;
     public GameObject Creation;
     public GameObject Corruption;
     public GameObject Divinity;
     public GameObject Plague;
     public GameObject Addiction;
         
     public bool hasWorm = true;
     public bool hasDazzler = false;
     public bool hasDisaster = false;
     public bool hasGrief = false;
     public bool hasCreation = false;
     public bool hasCorruption = false;
     public bool hasDivinity = false;
     public bool hasPlague = false;
     public bool hasAddiction = false;
     
     public Transform WeaponFire;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         ClickToFire();
     }
     
     void ClickToFire() {
         
         if(hasWorm = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Worm, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasDazzler = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Dazzler, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasDisaster = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Worm, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasGrief = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Grief, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasCreation = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Creation, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasCorruption = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Corruption, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasDivinity = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Divinity, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasPlague = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Plague, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
         
         if(hasAddiction = true && Input.GetButtonDown("FireTrigger")) {
             Instantiate(Addiction, WeaponFire.position, WeaponFire.rotation);
         }
         else {
             return;
         }
     }
 }
KillCount.cs
 using UnityEngine;
 using System.Collections;
 
 public class killCount : AllWeaponSystem {
     
     public int killCounter = 0;
     
     void Update() {
         weaponUpgrade();
     }
     
     void weaponUpgrade() {
         
         AllWeaponSystem weapon = new AllWeaponSystem();
         
         if(killCounter == 10) {
             weapon.hasWorm = false;
             weapon.hasDazzler = true;
         }
         
         if(killCounter == 20) {
             weapon.hasDazzler = false;
             weapon.hasDisaster = true;
         }
         
         if(killCounter == 35) {
             weapon.hasDisaster = false;
             weapon.hasGrief = true;
         }
 
         if(killCounter == 45) {
             weapon.hasGrief = false;
             weapon.hasCreation = true;
         }
 
         if(killCounter == 60) {
             weapon.hasCreation = false;
             weapon.hasCorruption = true;
         }
 
         if(killCounter == 75) {
             weapon.hasCorruption = false;
             weapon.hasDivinity = true;
         }
 
         if(killCounter == 99) {
             weapon.hasDivinity = false;
             weapon.hasPlague = true;
         }
 
         if(killCounter == 100) {
             weapon.hasPlague = false;
             weapon.hasAddiction = true;
         }
 
     }
     
 }
EnemyHealth.cs
 using UnityEngine;
 using System.Collections;
 
 public class enemyHealth : MonoBehaviour {
     
     public int maxHealth = 100;
     public int curHealth = 100;
     
     void OnCollisionEnter(Collision weaponParticle) {
         
         if(weaponParticle.gameObject.tag == "wormWeapon") {
             curHealth -= 1;
         }
 
         if(weaponParticle.gameObject.tag == "dazzlerWeapon") {
             curHealth -= 3;
         }
         
         if(weaponParticle.gameObject.tag == "disasterWeapon") {
             curHealth -= 5;
         }
         
         if(weaponParticle.gameObject.tag == "griefWeapon") {
             curHealth -= 9;
         }
         
         if(weaponParticle.gameObject.tag == "creationWeapon") {
             curHealth -= 15;
         }
         
         if(weaponParticle.gameObject.tag == "corruptionWeapon") {
             curHealth -= 20;
         }
         
         if(weaponParticle.gameObject.tag == "divinityWeapon") {
             curHealth -= 25;
         }
         
         if(weaponParticle.gameObject.tag == "plagueWeapon") {
             curHealth -= 28;
         }
         
         if(weaponParticle.gameObject.tag == "addictionWeapon") {
             curHealth -= 30;
         }
         
     }
 }
 
Ummm, I need an answer by the end of today guys! Please be a bit more quick!
be patient, this is not premium support.. I'll be happy to fix up your code for you, if someone doesn't beat me to it..
Answer by Seth-Bergman · Mar 10, 2013 at 09:29 AM
Let me introduce you to a great alternative to nine booleans: the ENUMERATION
here's a better version of your script:
 using UnityEngine;
 using System.Collections;
  
 public class AllWeaponSystem : MonoBehaviour {
 
 public GameObject[] Particles = new GameObject[9];
 
 enum ParticleTypes {Worm,Dazzler,Disaster,Grief,Creation,Corruption,Divinity,Plague,Addiction};
 
 public ParticleTypes currentParticle;
 
 public Transform WeaponFire;
  
 void Update() {
  
 if(Input.GetButtonDown("FireTrigger")) {
 Instantiate(particles[currentParticle], WeaponFire.position, WeaponFire.rotation);
      }
    }
 }
We first create a custom data type called "ParticleTypes", then we declare an instance of it like a normal variable.. each value has a corresponding int value, starting with 0:
Worm = 0
Dazzler = 1
Disaster = 2
etc
I also combine your nine game objects into an array.. you can add those by drag-n-drop (in order), it's much easier to manage an array..
so the second script would be:
 using UnityEngine;
 using System.Collections;
  
 public class killCount : AllWeaponSystem {
  
 public int killCounter = 0;
  
 void Update() {
 
  if(killCounter/10 > weapon.currentParticle) {
 weapon.currentParticle+=1;
      }
    }
 }
Or something like this...
(my c# is rusty, there may be errors...)
this should give you an idea, if it doesn't work off the bat...
Of course, this same strategy would also work with a simple int (at least so far).. but using an enum makes it much easier to follow your code..
while I am using the int values above, we could alternatively say something like:
 if(currentParticle == ParticleTypes.Worm) 
for example..
Your answer
 
 
             Follow this Question
Related Questions
Roguelike Elements... 1 Answer
Scripts of instantiated objects 1 Answer
Help with a different approach to instantiating weapons? 1 Answer
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer
How to count instantiated objects that passes a given point (transform.position.y) 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                