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
0
Question by Danao · Nov 02, 2014 at 10:48 AM · prefabarrayweaponshot

How to substitute an array for a public Gameobject?

Hello, I'm trying to get a weapon array to work so I can have my player shoot different prefabs. If I keep the public field as one prefab it works fine, I tried to add the array and it's not working. Can someone point me in the right direction? Thanks (and by the way I realize there is currently nothing to change the currentWeapon int from 0 to 1, but I've been doing it manually to test it)!

 // Launch projectile
 public class PlayerWeaponScript : MonoBehaviour
 {
     public Transform [] shotPrefab;// Projectile prefab for shooting
     public float shootingRate = 0.25f;// Cooldown in seconds between two shots
     private float shootCooldown;
     private int currentWeapon = 0;
 
     void Start()
     {
             shootCooldown = 0f;
     
 
             }
         void Update()
     {
         if (shootCooldown > 0)
         {
             shootCooldown -= Time.deltaTime;
         }
     }
     //Shooting from another script
     // Create a new projectile if possible
     public void Attack(bool isEnemy) {
                 if (CanAttack) {
                         if (currentWeapon == 0) {
                                 int shotIndex = 0;
                         } else if (currentWeapon == 1) {
                                 int shotIndex = 1;
                                 shootCooldown = shootingRate;
                                 var shotTransform = Instantiate (shotPrefab [shotIndex]) as Transform;// Create a new shot
                                 shotTransform.position = transform.position;// Assign position
                                 ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
                                 if (shot != null) {
                                         shot.isEnemyShot = isEnemy;
                                         gameObject.tag = "Enemy";
                                 }
                                 // Make the weapon shot always toward
                                 EnemyMoveScript move = shotTransform.gameObject.GetComponent<EnemyMoveScript> ();
                                 if (move != null) {
                                         move.direction = this.transform.right; // towards in 2D space is the right of the sprite
             
                                 }
                         }
                 }
         }
         
         public bool CanAttack// Is the weapon ready to create a new projectile?
     {
         get
         {
         return shootCooldown <= 0f;
         }
     }
 }
         
     
Comment
Add comment · Show 4
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 yashpal · Nov 02, 2014 at 11:29 AM 0
Share

@Danao diid you get any error? Or just generate same prefab every time?

avatar image Danao · Nov 02, 2014 at 12:33 PM 0
Share

Thanks for the reply yashpal. It's not generating any prefab when I use the array (though I can use a single public gameObject and it works fine for one weapon).

avatar image Kiwasi · Nov 03, 2014 at 03:11 AM 0
Share

$$anonymous$$essed with your formatting a bit. Need to be able to read it to help. :)

avatar image Danao · Nov 03, 2014 at 05:13 AM 0
Share

Thanks guys, you're awesome.

@Bored$$anonymous$$ormon: I'll wait to see your edits before I try something else and screw it up. Lol.

@Bunny83: I see what you are saying about currentWeapon, thanks :)

2 Replies

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

Answer by Danao · Nov 03, 2014 at 06:22 AM

Here is the code that finally worked for me (still changing the currentWeapon int manually), based on major help from BoredMormon and Bunny83:

 using UnityEngine;
 
 // Launch projectile
 public class PlayerWeaponScript : MonoBehaviour
 {
     public Transform [] shotPrefab;// Projectile prefab for shooting
     public float shootingRate = 0.25f;// Cooldown in seconds between two shots
     private float shootCooldown;
     private int currentWeapon = 0;
 
     void Start()
     {
             shootCooldown = 0f;
     }
         void Update()
     {
         if (shootCooldown > 0)
         {
             shootCooldown -= Time.deltaTime;
         }
     }
     //Shooting from another script
     // Create a new projectile if possible
     public void Attack(bool isEnemy) {
         if (CanAttack){
             if (currentWeapon == 0) {
             } else if (currentWeapon == 1) {
             }
             shootCooldown = shootingRate;
             var shotTransform = Instantiate (shotPrefab [currentWeapon]) as Transform;// Create a new shot
                                 shotTransform.position = transform.position;// Assign position
                                 ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
                                 if (shot != null) {
                                         shot.isEnemyShot = isEnemy;
                                         gameObject.tag = "Enemy";
                                 }
                                 // Make the weapon shot always toward
                                 EnemyMoveScript move = shotTransform.gameObject.GetComponent<EnemyMoveScript> ();
                                 if (move != null) {
                                         move.direction = this.transform.right; // towards in 2D space is the right of the sprite
             
                                 }
                         }
                 }
         public bool CanAttack// Is the weapon ready to create a new projectile?
     {
         get
         {
         return shootCooldown <= 0f;
         }
     }
 }
         
     

Comment
Add comment · 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
0

Answer by Kiwasi · Nov 03, 2014 at 03:18 AM

Looks like you need another } to close off the else if on line 29. Currently you are only firing if the second weapon is selected. A nested if might be better. Pseudo code

 public void Attack(bool isEnemy) {
      if (CanAttack){
          int shotIndex;
          if (currentWeapon == 0) {
              shotIndex = 0;
          } else if (currentWeapon == 1) {
              shotIndex = 1;
          }
          // Do attack as written
      }
 }

Note: If currentWeapon is always going to map directly onto shotIndex then there is no need to use shotIndex.

Edit: Moved scope of shotIndex up a level. See comments.

Comment
Add comment · Show 8 · 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 Danao · Nov 03, 2014 at 04:07 AM 0
Share

Thanks for the reply Bored$$anonymous$$ormon. I tried the pseudo code you provided and updated my original code posting accordingly, but it still seems I can only fire the second weapon. If shotIndex = 0, it should pull the prefab from element 0 in the array, correct?

avatar image Kiwasi · Nov 03, 2014 at 04:30 AM 0
Share

Your updated code still has the shooting inside the else if clause

avatar image Danao · Nov 03, 2014 at 04:34 AM 0
Share

Yes, looks like that is the problem. I tried to put the shooting in both the if and else if, but that messes with my definitions. I'm going to need to try something else. I'll see what I can come up with and come back to post with some (hopefully) working code.

avatar image Kiwasi · Nov 03, 2014 at 04:42 AM 0
Share

It should be in neither. Pay careful attention to the bracket position in my pseudo code.

avatar image Bunny83 · Nov 03, 2014 at 04:58 AM 1
Share

@Bored$$anonymous$$ormon:
You shouldn't declare the shotIndex variable inside the if block(s) or it will be out of scope when he wants to use it outside.

@Danao:
If shotIndex always equals currentWeapon, why don't you use currentWeapon directly as index?

Show more comments

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

28 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

Related Questions

Weapon Prefab 0 Answers

How to create Random Stats matching the weapons? 0 Answers

Trying to instantiate random enemy prefab from array 3 Answers

Resizeable gameObject array. 1 Answer

tracking ownership of shots 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