#My Tilemap Collider will not detect collisions through code?

1 messages · Page 1 of 1 (latest)

fallen jetty
#

For whatever reason (probably I fucked something up), my tilemap composite collider will not allow me to use this code?
I am honestly just stumped on what to do. Everything else works, but for some reason, this just doesn't...
If someone could help that would be extremely nice :D

public class Checkpoint : MonoBehaviour
{
    [SerializeField] private Transform originalSpawnPosition;

    protected Transform respawnPosition;
    [SerializeField] private GameObject previousRespawnPosition;
    [SerializeField] private GameObject player;

    private void Awake()
    {
        if (player == null)
            Debug.LogWarning("The Player game object has not been assigned to respawn point!");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            respawnPosition = transform;

            if (previousRespawnPosition == null)
                return;
            else
                previousRespawnPosition.SetActive(false);
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
            ReturnToCheckpoint();
    }

    public void ReturnToCheckpoint()
    {
        Debug.Log("Returned");

        if(respawnPosition == null)
            player.transform.position = originalSpawnPosition.transform.position;

        player.transform.position = respawnPosition.transform.position;
    }
}
public class Respawn : MonoBehaviour
{
    private Checkpoint checkpoint;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
            checkpoint.ReturnToCheckpoint(); //the error is here, it just says "nullreferenceexception: object reference not set to an instance of an object.
    }
}

Additionally I have put an image of my tilemap collider here too if that helps at all!
Thanks a bunch!

plucky sedge
fallen jetty
#

checkpoint is assigned in the respawn script

plucky sedge
fallen jetty
#
public class Respawn : MonoBehaviour
{
    private Checkpoint checkpoint; // <------- here :D 

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
            checkpoint.ReturnToCheckpoint(); //the error is here, it just says "nullreferenceexception: object reference not set to an instance of an object.
    }
}
#

i think that might not be what u are refering to tho

#

i probably just forgot something

plucky sedge
fallen jetty
#

OOOOOOH

#

ok, so I just need to assign it in like awake or smth?

plucky sedge
#

You can also directly do so in the inspector (drag n drop)

fallen jetty
#

ok good to know

#

but the checkpoint is a script, so what do i do there?

plucky sedge
#

drag in the slot the gameobject with the Checkpoint script you want to reference

#

did you not do this in the Checkpoint script already ?

#

its the same concepts

#

declare a value
assign a value

fallen jetty
#

i did, but i just forgot i guess

#

i've been working since like 6 am today

#

so like im a bit slower than usual

plucky sedge
#

ow. Time to take a break! coding exhausted is never good lol

fallen jetty
#

yeah, i just was hanging out with friends while waiting for some responses in this, so that was a good break, but yeah!

#

Btw, what do i do if there are multiple objects with the script?

plucky sedge
#

you assign it the object with the script you want to interact with, each one is its own instance of Checkpoint or whatever else

#

they have the same behaviors but they are different versions of it

fallen jetty
#

ah

#

got it

#

makes sense :D

#

thanks!

plucky sedge
fallen jetty
#

and it works like a charm immediately!

#

you are a life-saver!

plucky sedge
fallen jetty
#

Thank you so much! That saved a ton of time lol :D