#if anyone is free, I'm getting a null
1 messages · Page 1 of 1 (latest)
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;
}
}
what line
33
That line cannot throw a null reference error
when i click the down arrow in the editor it does
Show the error
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?
What is OneWayPlatform.cs line 39
Then we're done here
is the tag
Maybe show the entire script
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);
}
}
!code
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
Post using an external link so we can see line numbers
okay
And what is the issue of finding line 39 of that
Okay, so,
BoxCollider2D platfomCollider = currentOneWayPlatform.GetComponent<BoxCollider2D>();
currentOneWayPlatform is null
yeah
You can't get a Box Collider from nothing
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
What do you want this function to do when you aren't touching a platform
nothing i just dont want it to throw an error
So check if it's null before trying to do anything
if(!currentOneWayPlatform)
return;//do nothing```
and if it's null, don't do anything
so i just write currentOneWayPlatform = !currentOneWayPlatform
and put
if(!currentOneWayPlatform)
return;//do nothing
in update?
what
nvm
Just use an if statement. Check if it's not null before you try to use it
You'd just return early from the Update method or coroutine with yield return break if the reference was null
is this better dalphat
Pretty sure we're both saying the same thing