Triggering Wall Movement in Code

Hi All!

I’ve been trying to code a trigger that will happen when a player stands on a button trigger that will make an object(Wall) move up allowing the player to proceed through the level. When testing it the trigger seems to work in the console, but the wall itself is not moving. Any help would be greatly appreciated. :smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerWall : MonoBehaviour
{
   

  
   
    public Transform Wall;

    public float YTarget;

    [Tooltip("Enter Y position wall should end up to allow player to progress")]
    public float NewWallPos;

    private void Awake()
    {
       
    }

    private void Start()
    {
        YTarget = Wall.position.y + NewWallPos; // Taking YTarget Position of object and equal it to current Y pos
    }

    private void Update()
    {
      
    }

    // Makes the wall move a certain amount upward
    IEnumerator ActivateWall()
    {
        while (YTarget <= NewWallPos)
        {
            Wall.transform.Translate(Vector3.up * Time.deltaTime);
        }
        yield return null;
        Debug.Log("Wall up");
      
    }

    // When player collides with button wall will move
    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Player")
        {
            Debug.Log("Wall activated");
            StartCoroutine(ActivateWall());
        }
    }



}

Line 36 is a no-no: 100% of that while loop will instantly complete before another frame is rendered. It MUST have a yield return null; inside the loop to do things “over time.” Review coroutines for why.

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:

https://discussions.unity.com/t/839300/3

1 Like

That was really good advice! I moved the yield return null into the while loop. I sprinkled more Debug.Log() into the coroutine and it turns out the while loop itself is not being executed. When making the YTarget variable equal the Wall Position Y I added the NewWallPos to it making it so the While Loop never executed because the condition was already met.

Thank you so much! @Kurt-Dekker

1 Like

Yeah, you just gotta chase it right up the line, and Debug.Log() will always work.

It’s a great feeling when you put a Debug.Log() in somewhere that you THINK is running and it actually isn’t.

When this happens, I almost always say out loud in a faux southern American accent,

“Well, there’s yer problem!”