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
0
Question by Wuschel_ · Jul 06, 2017 at 12:38 AM · instantiateraycastcollidercollider2d

Unity Raycast2D ignored instantiated Prefab object

I'm currently making a small game for a university assignment. It's a simple 2D game where you shoot birds using the mouse.

I did collission detection by hand at first (check for x/y boundaries) but now I wanna use a collider.

So I added a CircleCollider2D to my objects and I perform the Raycast like so

 Vector3 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

 RaycastHit2D[] hits = Physics2D.RaycastAll (mousePosition, Vector3.forward, Mathf.Infinity);
 Debug.Log (hits.Length);

Now when I add my prefabs manually into my scene it works as expected. The log outputs 1, or even 2 if I have multiple birds on top of each other.

But my game instantiated a lot of birds randomly and they are not detected when performing the raycast. I already checked, they definitely have colliders.

The instantiation code looks like the following:

 Bird bird = Instantiate (birdPrefab, birdPosition, Quaternion.identity) as Bird;

I googled around for 2 hours now, but nothing could actually solve my problem.

I don't know if this is important, but the collision detection does not happen within the bird object's attached script.

I'm really cofused because it works with manually put objects, but not with instantiated ones. Has anybody experienced this before?

The mouse position is on (x, y, -10) and the birds are on (x, y, 0) and both the manually placed ones and the instantiated ones are direct children of the scene.

Comment
Add comment · Show 1
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 Jwizard93 · Jul 06, 2017 at 02:30 AM 0
Share

I'm unable to reproduce your issue. Your code is working fine in my project. Double check that the prefabs are instantiated with 2D colliders and not 3D colliders.

0 Replies

· Add your reply
  • Sort: 

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

110 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 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 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 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

Raycast causes game to crash when shooting up 1 Answer

how to move an object clone to mouse position, i dont understand ? 1 Answer

Cutting a Sprite 0 Answers

how to prevent an gameobject of instantiating if theres an collider where it is supposed to collide 1 Answer

What would be a good way to not have objects spawn inside one another? 2 Answers


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