Search Unity

mouse manager script not working!!!!!

Discussion in 'Scripting' started by abhinavsharma701, Sep 25, 2019.

  1. abhinavsharma701

    abhinavsharma701

    Joined:
    Sep 18, 2019
    Posts:
    5
    i created a mouse manager script learned from unity learn and i have double checked my script but its not working...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5.  
    6. public class Mouse_Manager : MonoBehaviour
    7. {
    8.     public LayerMask clickablePlayer;
    9.  
    10.     public Texture2D pointer;
    11.     public Texture2D target ;
    12.     public Texture2D doorway;
    13.     public Texture2D combat;
    14.     private bool Defaultvalue = false;
    15.    
    16.  
    17.    
    18.     private void Update()
    19.     {
    20.         RaycastHit hit;
    21.  
    22.         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50, clickablePlayer.value))
    23.         {
    24.             bool door = Defaultvalue;
    25.             if (hit.collider.gameObject.tag == "Doorway")
    26.             {
    27.                 Cursor.SetCursor(doorway, new Vector2(16, 16), CursorMode.Auto);
    28.                 door = true;
    29.             }
    30.             else
    31.             {
    32.                 Cursor.SetCursor(target, new Vector2(16, 16), CursorMode.Auto);
    33.             }
    34.  
    35.         }
    36.         else
    37.         {
    38.             Cursor.SetCursor(pointer, new Vector2(0,0) , CursorMode.Auto);
    39.         }
    40.     }
    41. }
    42.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome.
    Generally speaking, you want to include as much helpful information as possible.

    "It's not working" is basically useless in terms of helping you. What is not working? What is it supposed to do, what is it doing instead? Is it throwing errors? If so, which one, including the full error message (and if you, for example, only posted a part and not the full script, you'd want to include which line the error occurs on in the posted code example).
     
    abhinavsharma701 likes this.
  3. abhinavsharma701

    abhinavsharma701

    Joined:
    Sep 18, 2019
    Posts:
    5
    thankyou for replying yes i will provide you with full info.....

    i was trying to change cursor in game whenever cursor reaches to object for example a door icon when cursor is placed on door in environment.
    i take a empty game object as mouse manager and then added above script to it.... i creacted a new layer clickable and set my door to clickable layer and Doorway tag to door as required by code but when i compile all them and start game no cursor changes!!!
     
  4. abhinavsharma701

    abhinavsharma701

    Joined:
    Sep 18, 2019
    Posts:
    5
    compiler is showing this error.....
    NullReferenceException: Object reference not set to an instance of an object
    Mouse_Manager.FixedUpdate () (at Assets/Scripts/Mouse_Manager.cs:22)
     
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,326
    It looks like you don't have a Camera with the tag "MainCamera" in the scene. This causes Camera.main to return null, which causes the NullReferenceException.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Yeah, either the (main) camera does not exist, or you maybe forgot to assign clickablePlayer in the inspector.
     
  7. abhinavsharma701

    abhinavsharma701

    Joined:
    Sep 18, 2019
    Posts:
    5