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 Jurassic · Jul 18, 2013 at 03:35 AM · collisionprefabignorecollision

Code works fine until I turn the object it interacts with into a prefab.

Hi, I'm new to Unity and working on a 2.5D platformer as my first project. With some Google-fu and the help of already answered topics on this very forum, I've managed to figure out how to create platforms that the player can jump up onto from beneath and can also fall down through.

I have a box collider trigger volume childed to my platforms that turns off the collision between the player and the platform for jumping up beneath them, and the collision box doesn't hit the top of the platform so it only works when jumping from beneath - exactly as I want it to. This is the code I have attached to the trigger volume:

 using UnityEngine;
 using System.Collections;
 
 public class PlatformJumpThrough : MonoBehaviour {
     
     private Collider collis1;
     private Collider collis2;
     
     void Start () {
         
     }
     
     void OnTriggerEnter(Collider platformCollider) {
         platformCollider.gameObject.layer=4;
         var platform = transform.parent;
         collis1 = platformCollider.collider;
         collis2 = platform.collider;
         Physics.IgnoreCollision(collis1, collis2);            
     }
     
     void OnTriggerExit(Collider platformCollider) {
         platformCollider.gameObject.layer=0;        
         Physics.IgnoreCollision(collis1, collis2, false);    
     }
 
     void Update () {
     
     }
 }


For allowing the player to drop down through the platforms, I have a very similar code setup, except without the trigger volume. This code is attached to the player:

 using UnityEngine;
 using System.Collections;
 
 public class JumpDown : MonoBehaviour {
     
     private Collider collis1;
     private Collider collis2;
     public GameObject platform;
 
     void Start () {
         collis1 = gameObject.collider;
         collis2 = platform.collider;
     }
     
     void Update () {
         
         if(Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)){
             gameObject.layer=4;
             Physics.IgnoreCollision(collis1, collis2);                    
         }
         else if(Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow)){
             gameObject.layer=0;
             Physics.IgnoreCollision(collis1, collis2, false);                            
         }
     
     }
 }

The problems occur when I turn the platform & trigger volume into a prefab. I set the public GameObject platform in the JumpDown code to the prefab instead of just the one object, and the first code seems to function fine and works as expected, but when I try to fall through the platform I am greeted with this a crash and this error:

Ignore collision failed. Both colliders need to be activated when calling this IgnoreCollision UnityEngine.Physics:IgnoreCollision(Collider, Collider, Boolean) JumpDown:Update() (at Assets/JumpDown.cs:25)

The colliders seem to be active by default and the player is able to stand on the platforms just fine. It only seems to throw the error when I try to fall through. (On KeyDown S or KeyDown DownArrow) Any idea what could cause this issue? Thanks!

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
Best Answer

Answer by Adamcbrz · Jul 18, 2013 at 04:11 AM

Are you attaching the platform variable to the prefab that is in your scene/heirarchy or to the prefab in the project folder? I am assuming the one in the project folder. The prefab has to be instatiated and then you can reference the gameObject from the instatiation. A prefab is a sort of like a snapshot of a gameobject and doesn't provide you the full functionalit until you create an instance of the prefab.

Comment
Add comment · Show 1 · 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 Jurassic · Jul 18, 2013 at 05:46 AM 0
Share

I'm still not sure what the exact issue here was but I realized I was doing this in a completely roundabout and inefficient manner and fixed up my code myself. Thanks though! :) If you're curious, here's the fully functioning code:

This is attached to a trigger volume below the platform. It checks if the player is colliding with the volume and moves the platform (but not the trigger volumes attached to it) into a non-colliding layer until the player leaves the volume.

 using UnityEngine;
 using System.Collections;
 
 public class PlatformJumpThrough : $$anonymous$$onoBehaviour {
 
     private Collider platform;    
     
     void Start () {
         
     }
     
     void OnTriggerEnter(Collider platformCollider) {
         platform = transform.parent.collider;        
         platform.gameObject.layer=9;        //$$anonymous$$oves the platform into a layer with no collision so the player can pass through
     }
     
     void OnTriggerExit(Collider platformCollider) {
         platform.gameObject.layer=0;        //$$anonymous$$oves the platform back when the player leaves the trigger
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }


And this is attached to a trigger volume sitting right atop the platform. It's only "active" if the player is colliding with it, and if they are, they can press "Down" to fall through the platform by moving it to a non-collidable layer.

 using UnityEngine;
 using System.Collections;
 
 public class PlatformFallThrough : $$anonymous$$onoBehaviour {
 
     private Collider platform;
     public bool playerStanding = false;
     
     void Start () {
         platform = transform.parent.collider;        
     }
     
     void OnTriggerEnter(Collider platformCollider) {
         platform = transform.parent.collider;            
         playerStanding = true;
     }
     
     void OnTriggerExit(Collider platformCollider) {
         playerStanding = false;        
     }
     
     // Update is called once per frame
     void Update () {
                                             //Allows the player to press "Down" to fall through platforms
         if(playerStanding && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow)){
             platform.gameObject.layer=9;                
         }
         else if(!playerStanding && Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.S) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.DownArrow)){
             platform.gameObject.layer=0;        
         }
     }
 }

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

16 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

Related Questions

ignoreCollision information disappearing for prefab'd objects 0 Answers

Need help ignoring collision 1 Answer

Collision between two moving clones 0 Answers

Can prefabs interact with scroll rect? 0 Answers

How do i get some objects to ignore collision with a specific object? 10 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