Button switching color in unity

Viewed 48

I am very new to unity and coding in general, I am trying to create a button in unity that eventually opens a door in my game, but I immediatle ran into a problem, when I collide with the button it doesnt switch color, Im not sure if something is wrong with my code or my settings in unity

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

public class Button : MonoBehaviour
{
    [SerializeField]
    GameObject switchOn;

    [SerializeField]
    GameObject switchOff;

    public bool isOn = false;

    void Start () 
    {
        gameObject.GetComponent<SpriteRenderer>().sprite = switchOff.GetComponent<SpriteRenderer>().sprite;
    }

    void OnTriggerEnter2D(Collider2D col) 
    {
        gameObject.GetComponent<SpriteRenderer>().sprite = switchOn.GetComponent<SpriteRenderer>().sprite;

        isOn = true;
    }
}

I have 2 different button textures put on the on and off gameObjects a green one and a red one, does anyone know if something is wrong with the code

1 Answers

debug a line in your on collision enter, likely its not getting called. Make sure your player has a rigidbody and a collider and make sure your door has a collider. Also make sure trigger is checked on your player collider.

Related