Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
-1
Question by DubstepDragon · Mar 09, 2013 at 12:20 PM · instantiateweaponcountkillroguelike

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;
         }
         
     }
 }
 
Comment
Add comment · Show 2
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 DubstepDragon · Mar 10, 2013 at 08:43 AM 0
Share

Ummm, I need an answer by the end of today guys! Please be a bit more quick!

avatar image Seth-Bergman · Mar 10, 2013 at 09:10 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · 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 Seth-Bergman · Mar 10, 2013 at 10:28 AM 0
Share

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

avatar image DubstepDragon · Mar 13, 2013 at 07:27 PM 0
Share

You are awesomely godlike. Thx so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 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

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


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