Hi, I’m currently trying to create an infinitely playable vertical platformer (doodle jump style), but I ran into a problem when trying to create platforms above the camera. This code works when using any prefab but the platform one.
I tried to replace the variable with a different prefab and it works. Only when I add the same components as the platform it wont, here is an image of the platform in the inspector:
Never compare floating point quantities for equality; this is likely true 100% of the time, as two floating point numbers are almost never equal except when set to constants, and even then, don’t trust it.
Beyond that, you must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Thank you very much for the response. I tried debugging with Debug.Log() previously and the code runs fine, only when I replace the prefab with the platform it wont spawn. When I set up a test object without the platform behavior script, It spawns completely fine. Here’s the platform behavior script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformCollider : MonoBehaviour
{
GameObject playerGroundCheck;
[SerializeField] GameObject gameCamera;
private void Start()
{
playerGroundCheck = GameObject.FindGameObjectWithTag("Player Ground Check");
gameCamera = GameObject.FindGameObjectWithTag("MainCamera");
}
private void Update()
{
killPlatform();
var platformGroundCheck = transform.GetChild(0).gameObject;
var bcComp = GetComponent<BoxCollider2D>();
if (playerGroundCheck != null)
{
if (playerGroundCheck.transform.position.y > platformGroundCheck.transform.position.y && gameObject.GetComponent<BoxCollider2D>() == null)
{
BoxCollider2D Bc = gameObject.AddComponent(typeof(BoxCollider2D)) as BoxCollider2D;
var bcCompcol = GetComponent<BoxCollider2D>();
bcCompcol.size = new Vector2(1, 0.2f);
bcCompcol.offset = new Vector2(0, 0.4f);
}
else if (playerGroundCheck.transform.position.y < platformGroundCheck.transform.position.y)
{
Destroy(bcComp);
}
}
}
private void killPlatform()
{
if (Vector2.Distance(transform.position, gameCamera.transform.position) > 6.5f)
{
Destroy(gameObject);
}
}
}
Just wondering if there is anything in this script that is preventing it from spawning. I also got rid of the transform.position.y != 0;
Thanks for the suggestion, I figured out that I was spawning it too far from the camera and it was automatically destroying itself due to the destroy function for further down platforms.