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 jeffpk · Apr 29, 2012 at 02:43 AM · physicsraycastlayer

Unable to set collision layer from scrip

Hi Guys.

I want to make a collision just against one kidn of object in my world, a "Tile". I have defined a Tile layer (9) but the following fails:

When creating the tiile:

            GameObject newTile = 
         (GameObject)Instantiate(
             trec.GetPrefab(),
             new Vector3(rec.x*10,rec.e,rec.y*10),Quaternion.Euler(new Vector3(-90,((int)rec.facing)*90,0)));
     newTile.transform.parent = rootObject.transform;
     GameObjectUtils.SetShader(newTile,noCullShader); 
     tileGrid[rec.x,rec.y] = newTile;
     newTile.layer=LayerMask.NameToLayer("Tile");
     StaticBatchingUtility.Combine(new GameObject[]{newTile}, rootObject);

And when doing a collision test:

        if (Physics.Raycast(ray,out hit, Mathf.Infinity,1<<LayerMask.NameToLayer("Tile"))){
         tileMap.SetStartPoint(hit.point);
        }

For grins I tried changing the first code to

 newTile.layer=LayerMask.NameToLayer("Ignore Raycast");

And removed the layer mask from the raycast, and I got a hit.

It really looks like setting the layer in code just isnt working. Why not? is there some special magic about objects created by instantiating prefabs?

Comment
Add comment · Show 5
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 Owen-Reynolds · Apr 29, 2012 at 08:00 AM 0
Share

For the first part, do you mean it fails to set the layer (you check newTile and the layer is still 0)? NameToLayer returns -1 if you spell it wrong, etc... -- maybe Debug.Log for a test?

But, unless you're changing layers, why not just set the prefab tile to layer Tile?

avatar image jeffpk · Apr 29, 2012 at 03:22 PM 0
Share

I could, but there is a small question of correctness in that the tiles are comign from abundle that is made by a different project. So the loop is "open" in the sense that the "Tile" layer has to be set identicle in both projects. By setrting it in the instantiation project there is no way they can be diffrent.

Besides, it just annoys me that thius doesnt work. It seems like it should...

avatar image Owen-Reynolds · Apr 29, 2012 at 05:09 PM 0
Share

I think changing layers of Instantiated objects does work and something else in your code is the problem.

I know changing layers of pre-existing GO's through layer=new0-31LayerNum works fine -- the very next frame raycasts react to the new layer. There could be some odd glitch specific to prefabs and layers, but I doubt it.

avatar image jeffpk · Apr 29, 2012 at 05:16 PM 0
Share

Unfortunately, I treid goign to set the layer on the prefab in the editor, but these are imported collada assets and the entire "Imported Object" section of its properties is greyed out and un settable :(

avatar image Owen-Reynolds · Apr 29, 2012 at 08:29 PM 0
Share

Well, you can turn a locked imported model into one of your own prefabs by dragging it into the scene, the dragging it back to an empty prefab.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast not working 2 Answers

How to make my raycast also hit deactive objects WITHOUT showing the object? 1 Answer

Hovercraft Physics Problems 1 Answer

define physics collision layers and set them dynamically 0 Answers

How do i make my own ignore raycast user layer? 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