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 Hyperion · Aug 05, 2013 at 12:56 AM · instantiatearrayprojectilecycle

Projectile Selection through Array

Hi everyone,

I've made my vehicle able to select 2 types of rockets based on whether I press "1" or "2" (and space bar to shoot). Except I wish to add more types of projectiles later on and I know the code will definitely get messy without an array, which I am currently not using. How may I implement projectile selection through an array? I know how to make an array for the projectiles, but I can't manage to reference the projectile inside the array into the Instantiation script.

I appreciate your help- Hyperion

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 robertbu · Aug 05, 2013 at 01:30 AM

I'm not sure where you have the stumbling block, so I'll take a shot:

At the top of the file you can do:

 var prefab : GameObject[];

You can click on the triangle to the left of the variable, set the size of the array, and then drag and drop your prefabs into the array.

To fire the projectile you can do something like:

 function FireProjectile(index : int) {
   var go = Instantiate(prefab[index], transform.position, transform.rotation);
   go.rigidbody.AddForce(transform.forward * force_factor);
 }

There are several ways to translate keys to specific values to pass into fireProjectile. One simple way would be to use a switch() statement.

Comment
Add comment · Show 4 · 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 Hyperion · Aug 05, 2013 at 02:51 AM 0
Share

PART 1 OF CO$$anonymous$$$$anonymous$$ENT: Well, that's not quite what I was going for... Here's my current attempt at the issue in code:

 var projectiles:GameObject[];
 
 function Start()
 {
     SelectProjectile(0);
 }

function FixedUpdate() {
//Projectile Selection if (Input.Get$$anonymous$$eyDown("1")) SelectProjectile(0); if (Input.Get$$anonymous$$eyDown("2")) SelectProjectile(1); if (Input.GetButtonDown("Jump") && canShoot){

         var projectile=Instantiate(projectiles[index], transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
         
         //tags projectile, gets speed of vehicle and multiplies it by 3000 for the RPG
         if (projectiles[index]==RPG){
             projectile.gameObject.tag="projectile";
             projectile.rigidbody.AddForce(transform.forward*4000);
         }
         if (projectiles[index]==RPBR){
             projectile.gameObject.tag="BouncyRocket";
             projectile.rigidbody.AddForce(transform.forward*50000);
             projectile.rigidbody.AddForce(transform.up*7000);
         }
 
 }

}

 function SelectProjectile(index : int) {
     for (var obj:GameObject in projectiles)obj.SetActive(false);
     projectiles[index].SetActive(true);
 }

Except it has a problem: projectiles index is unidentified in an error message.

avatar image Hyperion · Aug 05, 2013 at 02:59 AM 0
Share

PART 2 OF CO$$anonymous$$$$anonymous$$ENT: How about I just show you my code that works without an array? $$anonymous$$aybe you'll be able to understand what I meant specifically...

 //the 2 projectile types I have so far
 var RPG:Transform;
 var RPBR:Transform; 
 
 //variable for storing current projectile being used/to be used
 var myProjectile;
 
 function Start()
 {
     myProjectile=RPG;
 }
 
 //Fixed Update because of State$$anonymous$$achine, not a problem
 function FixedUpdate()
 {
 //Projectile Selection
     if (Input.Get$$anonymous$$eyDown("1")) myProjectile=RPG;
     if (Input.Get$$anonymous$$eyDown("2")) myProjectile=RPBR;
 
 if (Input.GetButtonDown("Jump") && canShoot){
         
                 var projectile=Instantiate(myProjectile, transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
         
     
         if (myProjectile==RPG){
             projectile.gameObject.tag="projectile";
             projectile.rigidbody.AddForce(transform.forward*4000);
         }
         if (myProjectile==RPBR){
             projectile.gameObject.tag="BouncyRocket";
             projectile.rigidbody.AddForce(transform.forward*50000);
             projectile.rigidbody.AddForce(transform.up*7000);
         }
 }
 }

By the way, thank you for helping me with the code, I don't mean to be a nuisance but I'm bad with arrays.

avatar image robertbu · Aug 05, 2013 at 03:23 AM 0
Share

The concept is the same. Here is a quick rewrite of your code to show you what I mean:

 var projectiles : Transform[];
 
 enum ProjType { RPG, RPBR }
 
 var currProj = ProjType.RPG;
 
 var myProjectile;
 
 //Fixed Update because of State$$anonymous$$achine, not a problem
 function FixedUpdate()
 {
 //Projectile Selection
     if (Input.Get$$anonymous$$eyDown("1")) currProj=ProjType.RPG;
     if (Input.Get$$anonymous$$eyDown("2")) currProj=ProjType.RPBR;
  
     if (Input.GetButtonDown("Jump") && canShoot){
           var projectile=Instantiate(projectiles[currProj], transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
  
         switch(currProj) {
         case ProjType.RPG:
             projectile.gameObject.tag="projectile";
             projectile.rigidbody.AddForce(transform.forward*4000);
             break;
         case ProjType.RPBR:
             projectile.gameObject.tag="BouncyRocket";
             projectile.rigidbody.AddForce(transform.forward*50000);
             projectile.rigidbody.AddForce(transform.up*7000);
             break;
         }
    
     }
 }

You will have to drag and drop your prefabs into the projectiles array in the correct order.

avatar image Hyperion · Aug 05, 2013 at 05:00 PM 0
Share

Thank you very much! It works.

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

14 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

Related Questions

Cycle Through GameObjects in Array Issue. 1 Answer

How to cycle through targets? 2 Answers

IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers

Activating Bool Array in sequence. 2 Answers

Array with game object: object reference not set to an instance of an object 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