- Home /
sortingLayer not working in almost all the scenes
Hello Guys,
I am new in the forum and almost new in unity. I currently having a issue with changin the sortinglayer of a door when i click on. The main purpuse to change the order to back and show sprite with a open Door. The script i made works great in one of the scenes but its not working in other two. I am using the new input method for the mouse clicking, so when i click on an object with a 2d collider it will be detected by the ray i create and show a message like "hit door" and change the order hidden the closed door sprite and showing the open door sprite. This method works in the main scene but it does not in other with the same structure. In the other scenes is only showing the message in the console. Here is the clickdoor script:
public class ClickDoor : MonoBehaviour
{
float Order;
public void Update()
{
}
public void OnClickDoor()
{
Order = gameObject.GetComponent<SpriteRenderer>().sortingOrder;
//if (gameObject.activeSelf == true)
//if (gameObject.GetComponent<SpriteRenderer>().sortingOrder == 1)
if (Order == 1)
{
gameObject.GetComponent<SpriteRenderer>().sortingOrder = -3;
//gameObject.SetActive(false);
}
if (Order == -3)
{
gameObject.GetComponent<SpriteRenderer>().sortingOrder = 1;
//gameObject.SetActive(true);
}
//Debug.Log("Hit Door");
}
And here is the part of the function from cursorcontrol script:
private void DetectObject()
{
Ray ray = mainCamera.ScreenPointToRay(controls.Mouse.Position.ReadValue<Vector2>());
// RaycastHit2D hits2D = Physics2D.GetRayIntersection(ray);
// if (hits2D.collider != null)
// {
// if (hits2D.collider.tag == "Door")
// {
// Debug.Log("2D Hit:" + hits2D.collider.tag);
//gameObject.SetActive(false);
//hits2D.collider.gameObject.GetComponent<ClickDoor>().OnClickDoor();
//} else
//{
// gameObject.SetActive(true);
//}
//}
//}
RaycastHit2D[] hits2DAll = Physics2D.GetRayIntersectionAll(ray);
for (int i = 0; i < hits2DAll.Length; ++i)
{
if (hits2DAll[i].collider != null)
{
if (hits2DAll[i].collider.name == "Door")
{
//Debug.Log("Hit Door");
hits2DAll[i].collider.gameObject.GetComponent<ClickDoor>().OnClickDoor();
}
if (hits2DAll[i].collider.name == "DoorOpen")
{
//Debug.Log("Hit DoorOpen");
hits2DAll[i].collider.gameObject.GetComponent<ClickDoorScummBar>().OnClickDoorScummBar();
}
if (hits2DAll[i].collider.name == "Kitchen_Pier_Door_Open")
{
//Debug.Log("Hit DoorOpen");
hits2DAll[i].collider.gameObject.GetComponent<ClickDoorKitchen>().OnClickDoorKitchen();
}
// Debug.Log("2D Hit All:" + hits2DAll[i].collider.tag);
}
}
}
Please if have time to take a look and help me i will be really appreciate it. Thanks you a lot in advance Best Regards
Your answer
Follow this Question
Related Questions
Prevent Raycast on reacting to GUI input? 2 Answers
Noob Raycast question 0 Answers
Confused as to how my RaycastHit script works 1 Answer
How to make a laser beam effect with raycast? 1 Answer
Raycast and Layer Masks 1 Answer