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 redcap · Apr 22, 2015 at 09:29 PM · instantiateprefabvariablescustom

When I instantiate a prefab, I want to assign it custom values.

I have a very simple code that fires a bullet when I hit spacebar. I want to assign these bullets values based on the ships current level.

Here is my code thus far:

 void Shoot (){
         if(Input.GetKeyDown(KeyCode.Space)){
             Instantiate(bullet1, gunPosition1.position, transform.rotation);
             Instantiate(bullet1, gunPosition2.position, transform.rotation);
             Instantiate(bullet1, gunPosition3.position, transform.rotation);
             Instantiate(bullet1, gunPosition4.position, transform.rotation);
             Instantiate(bullet1, gunPosition5.position, transform.rotation);
             Instantiate(bullet1, gunPosition6.position, transform.rotation);
         }
     }


How would I change it so that I could plug in the player level into the bullets?

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
0

Answer by Eluate · Apr 22, 2015 at 09:33 PM

You should be assigning the variables after you instantiate it. For example:

     if(Input.GetKeyDown(KeyCode.Space)){
              int playerLevel = 1;
              GameObject bullet = (GameObject)Instantiate(bullet1, gunPosition1.position, transform.rotation);
              bullet.getComponent<BulletScript>().level = playerlevel;
     }
Comment
Add comment · Show 3 · 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 redcap · Apr 23, 2015 at 01:03 AM 0
Share

Awesome, that indeed works perfectly. A new issue has come because of this though. When I try to insantiate multiple bullets at once it will only do one. Here is my code if you can help.

 void Shoot (){
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
 
             GameObject bullet1 = (GameObject)Instantiate(bullet, gunPosition1.position, transform.rotation);
 
 
             GameObject bullet2 = (GameObject)Instantiate(bullet, gunPosition2.position, transform.rotation);
 
 
             GameObject bullet3 = (GameObject)Instantiate(bullet, gunPosition3.position, transform.rotation);
         
 
             GameObject bullet4 = (GameObject)Instantiate(bullet, gunPosition4.position, transform.rotation);
         
 
             GameObject bullet5 = (GameObject)Instantiate(bullet, gunPosition5.position, transform.rotation);
 
 
             GameObject bullet6 = (GameObject)Instantiate(bullet, gunPosition6.position, transform.rotation);
 
             bullet1.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet1.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
             bullet2.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet2.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
             bullet3.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet3.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
             bullet4.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet4.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
             bullet5.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet5.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
             bullet6.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
             bullet6.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
         }
     }
avatar image Eluate · Apr 23, 2015 at 02:44 AM 0
Share

@redcap, that seems to be a bug. Also, you should consider using for loops and arrays ins$$anonymous$$d of making one entry for each bullet.

avatar image Belangia · Apr 23, 2015 at 04:52 AM 0
Share

You could use a for loop or recycle the statement.

example:

   GameObject bullet;
 
 bullet= (GameObject)Instantiate(bullet, gunPosition1.position, transform.rotation);
  
  bullet.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
  bullet.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
 bullet= (GameObject)Instantiate(bullet, gunPosition2.position, transform.rotation);
  
  bullet.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
  bullet.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
 bullet= (GameObject)Instantiate(bullet, gunPosition3.position, transform.rotation);
  
  bullet.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
  bullet.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
 bullet= (GameObject)Instantiate(bullet, gunPosition4.position, transform.rotation);
  
  bullet.GetComponent<PhoenixBullet>().skillLevel = GetComponent<ShipStats$$anonymous$$anager>().skill1Level;
  bullet.GetComponent<PhoenixBullet>().playerReference = GetComponent<ShipStats$$anonymous$$anager>().player;
 
 
 and so on. The Idea is to use as few new variables as you can. the creation process does grab memory. Or as suggested above use a for loop. 
 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate prefab with different variables 0 Answers

Variable not Assigned 1 Answer

Instantiate individual components of a prefab? 1 Answer

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 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