- Home /
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!
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.
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;
}
}
}