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 Staffiend · Sep 03, 2013 at 06:55 PM · gameobjectarrayswapcycle

Swap and cycle through game objects using an array

I am trying to swap and cycle through game objects. I have an array of game objects that are projectile attacks. I want the user to be able to press a button and swap out the current three game objects with the next three game objects in the array. When the last three are reached, I want to start the cycle over again. Basically it is like switching weapon loadouts on the fly. I have tried a few ways to do this (my switch statement failed horribly), but none have worked so far. Now I am running low on ideas. Any suggestions/tips/hints for how to handle something like this will be extremely helpful. Below is some code to look at if it helps you to get an idea of what I am trying to do.

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     
     
     public GameObject[] attackArray = new GameObject[6];
     
     
     public int shotPosX;
     public int shotPosY;
     
     
 
 
     // Use this for initialization
     void Start () {
         
         
     
     }
     
     // Update is called once per frame
     void Update () {
         
         
         GameObject attack = attackArray[0];
         GameObject attackDown = attackArray[1];
         GameObject attackUp = attackArray[2];
         
         if(Input.GetButtonUp("Fire1") == true)
         {
             Instantiate(attack, new Vector3(transform.position.x + shotPosX, transform.position.y - shotPosY, transform.position.z), transform.rotation);
         }
         
         if(Input.GetButtonUp("Fire2") == true)
         {
             Instantiate(attackUp, transform.position, transform.rotation);
         }
         
         if(Input.GetButtonUp("Fire3") == true)
         {
             Instantiate(attackDown, transform.position, transform.rotation);
         }
     
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by Vonni · Sep 03, 2013 at 07:51 PM

This wont compile :P But should be a reasonable way of doing it. You could ofcourse also just have 3 arrays, 1 for each weapon "slot?".

 int collectionSet = 0; // When zero the first three gameobjects in your array will be used as fire 1,2,3...
 public GameObject[] attackArray = new GameObject[6];
 
 update ()()()()(
 
 if(hit button that switches set of weapons)
   if(collectionSet is attackArray.Length - 2)
      collectionSet = 0;
   else
      collectionSet += 3;
 
 if( hit fire1)
 Instantiate(attack[collectionSet] // attack.... // etc....
 if( hit fire2)
 Instantiate(attack[collectionSet + 1] // attackUp.... // etc....
 if( hit fire3)
 Instantiate(attack[collectionSet + 2] // attackDown.... // etc....
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 Staffiend · Sep 04, 2013 at 03:44 PM 0
Share

Thank you very much. This was extremely helpful. With a very slight adjustment, this works like a charm for what I wanted to do.

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

17 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

Related Questions

hi, Im new in unity and I'm searching a way to swap two different 2d gameobjects on the same position when its clicked the object 1 to obeject 2.? ,hi guys, 1 Answer

Checking if a position is occupied in 2D? 1 Answer

Get all other GameObjects (not the one script is attached to) 2 Answers

Adding GameObject to GameObject[] array 1 Answer

Array out of index? 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