Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zlambe · Dec 06, 2015 at 08:52 PM · c#2dinstantiateprefabclone

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1]

So I'm making a 2D Space Shooter game, and I'm having my player shoot lots of types of bullets by using Instantiate to clone from a prefab, but the bullet keeps damaging my ship because even though the prefab is set to my "Player" layer (as well as my ship) and Player/Player collision is turned off, the bullet still hits me, which I eventually discovered is because it's cloning it onto the default layer instead of Player.

Is there any way I can force the bullet to clone onto the correct layer?

My full C# PlayerShooting.cs script (which is on the player) is this, in case it helps:

 public class PlayerShooting : MonoBehaviour {
 
     public GameObject bulletPrefab;
     public float fireDelay;
     float cooldownTimer = 0;
 
     void Update () {
         cooldownTimer -= Time.deltaTime;
 
         if (Input.GetButtonDown ("Fire1") && cooldownTimer <= 0) {
             // Bullet fires:
             gameObject.layer = 10;
             Instantiate(bulletPrefab, transform.position, transform.rotation);
             gameObject.layer = 8;
             Debug.Log("Fired!");
             //Cooldown Timer is reset:
             cooldownTimer = fireDelay;
         }
     }
 }
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 vintar · Dec 06, 2015 at 09:08 PM 0
Share
 gameObject.layer = Layer$$anonymous$$ask.NameToLayer("Player");


Try that.

avatar image Zlambe vintar · Dec 06, 2015 at 09:56 PM 0
Share

Where do I put it in the script? I tried putting the bullet onto the player layer in my bullet movement script within Start() but by the time that script had the chance to run the bullet had of course been created, then destroyed by damaging my ship...

avatar image vintar · Dec 06, 2015 at 10:02 PM 0
Share
 GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;
 bullet.layer = Layer$$anonymous$$ask.NameToLayer("Player");

Still not sure why it spawns it onto the default layer though...

avatar image Zlambe vintar · Dec 07, 2015 at 09:48 PM 0
Share

Just added that then realised that whenever I press play it switches every object onto the default layer, not just the bullet. That seems even weirder... Any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by phil_me_up · Dec 08, 2015 at 12:00 PM

As Vintar said above, you need to make sure you are setting the layer on your instantiated object.

However, as a general rule you don't want to be instantiating a bullet every time one is fired, instead you should consider making a bullet pool containing a number of bullets (depending on how many you expect to see on screen at any one time) and simply enable or disable these as you need them. A quick method for this is to just have an array of bullet game objects created. When you need to fire, find the next currently disabled bullet, enable it, position it correctly and then rather than destroying it when you don't need it anymore, just disable it.

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

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

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

How to move Instantiated 2D objects by 0.5 using arrows(or mouse) 1 Answer

Instantied 2D Prefab Is Invisible 1 Answer

How to move Instantiated 2D objects by 0.5 using arrows 1 Answer

Instantiated clones arent behaving the same as the prefab 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