#if anyone is free, I'm getting a null

1 messages · Page 1 of 1 (latest)

fallen prism
#

heres the code

#

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("OneWayPlatform"))
        {
            currentOneWayPlatform = collision.gameObject;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("OneWayPlatform"))
        {
            currentOneWayPlatform = null;
        }
    }
fallen prism
#

33

tired island
#

wow

#

thanks

#

very useful

fallen prism
#

currentOneWayPlatfor = null

#

sorry

#

i was on visual studio

tired island
fallen prism
#

when i click the down arrow in the editor it does

tired island
#

Show the error

fallen prism
#

NullReferenceException: Object reference not set to an instance of an object
OneWayPlatform+<DisableCollision>d__5.MoveNext () (at Assets/OneWayPlatform.cs:39)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <685c48cf8f0b48abb797275c046dda6a>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
OneWayPlatform:Update() (at Assets/OneWayPlatform.cs:17)

#

with a ss?

tired island
fallen prism
#

i have no idea

#

the only thing i have named OneWayPlatform

tired island
fallen prism
#

is the tag

cloud matrix
#

Maybe show the entire script

fallen prism
#

that is

#

not

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

public class OneWayPlatform : MonoBehaviour
{

    private GameObject currentOneWayPlatform;

    [SerializeField] private BoxCollider2D playerCollider;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            StartCoroutine(DisableCollision());
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("OneWayPlatform"))
        {
            currentOneWayPlatform = collision.gameObject;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("OneWayPlatform"))
        {
            currentOneWayPlatform = null;
        }
    }

    private IEnumerator DisableCollision()
    {
        BoxCollider2D platfomCollider = currentOneWayPlatform.GetComponent<BoxCollider2D>();

        Physics2D.IgnoreCollision(playerCollider, platfomCollider);
        yield return new WaitForSeconds(0.25f);
        Physics2D.IgnoreCollision(playerCollider, platfomCollider, false);
    }
}
polar flumeBOT
cloud matrix
#

Post using an external link so we can see line numbers

fallen prism
#

okay

tired island
#

And what is the issue of finding line 39 of that

fallen prism
tired island
#

Okay, so,

BoxCollider2D platfomCollider = currentOneWayPlatform.GetComponent<BoxCollider2D>();

currentOneWayPlatform is null

fallen prism
#

yeah

tired island
#

You can't get a Box Collider from nothing

fallen prism
#

i know that why it only works when touching the platform

#

im wondering a way so that it doesnt use null

#

and throw an error when i hit the down arrow not touching the one way platform

tired island
#

What do you want this function to do when you aren't touching a platform

fallen prism
#

nothing i just dont want it to throw an error

tired island
cloud matrix
#
if(!currentOneWayPlatform)
    return;//do nothing```
tired island
#

and if it's null, don't do anything

fallen prism
#

so i just write currentOneWayPlatform = !currentOneWayPlatform

#

and put

if(!currentOneWayPlatform)
return;//do nothing

#

in update?

tired island
#

what

fallen prism
#

nvm

tired island
#

Just use an if statement. Check if it's not null before you try to use it

cloud matrix
#

You'd just return early from the Update method or coroutine with yield return break if the reference was null

cloud matrix
#

Pretty sure we're both saying the same thing