Project: Gofile - Free Unlimited File Sharing and Storage
I’m trying to make a simple cabinet that opens and closes by enabling and disabling the sprite renderer.
I couldn’t find anywhere how to do it.
Here’s my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpeningAndClosing : MonoBehaviour {
public GameObject closedCabinet;
public GameObject openedCabinet;
public BoxCollider2D boxColClosed;
public BoxCollider2D boxColOpened;
private void Awake()
{
boxColClosed = closedCabinet.GetComponent<BoxCollider2D>();
boxColOpened = openedCabinet.GetComponent<BoxCollider2D>();
}
private void Update()
{
if (closedCabinet.GetComponent<Renderer>().enabled)
{
openedCabinet.GetComponent<Collider2D>().enabled = false;
}
else if (closedCabinet.GetComponent<Renderer>().enabled == false)
{
openedCabinet.GetComponent<Collider2D>().enabled = true;
}
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider.boxColClosed) || hit.collider.boxColOpened)
{
closedCabinet.GetComponent<Renderer>().enabled = !closedCabinet.GetComponent<Renderer>().enabled;
openedCabinet.GetComponent<Renderer>().enabled = !openedCabinet.GetComponent<Renderer>().enabled;
}
}
}
}
Thank you.