Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Unity suddenly can't detect clicks

Discussion in 'Scripting' started by CubeFlix, Mar 9, 2018.

  1. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    So, I was working on a project, and I have a tree that will disappear if the player clicks on it.
    Then, I imported a legacy asset, and now I can't detect it anymore.
    Is the problem the new asset, or just Unity. And if so, how do I fix it?

    Here is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TreesScript : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.     public GameObject woodSource;
    9.     private GameObject newTree;
    10.     private GameObject KSource;
    11.     public AudioClip treeSound;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         player = GameObject.Find("FPSController");
    16.         woodSource = GameObject.Find("WoodSource");
    17.         KSource = GameObject.Find("KSource");
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.        
    23.     }
    24.     void OnMouseDown()
    25.     {
    26.         gameObject.transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - 10, transform.localPosition.z);
    27.         gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = false;
    28.         gameObject.transform.GetChild(1).GetComponent<MeshRenderer>().enabled = false;
    29.         woodSource.GetComponent<AudioSource>().clip = treeSound;
    30.         woodSource.GetComponent<AudioSource>().Play();
    31.         Debug.Log("Got Some Wood!");
    32.         player.GetComponent<MainBehavioursScript>().wood += 5;
    33.         player.GetComponent<MainBehavioursScript>().score += 10;
    34.         Invoke("Respawn", 60);
    35.     }
    36.  
    37.     public void Respawn()
    38.     {
    39.         Debug.Log("Respawned");
    40.         gameObject.transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + 10, transform.localPosition.z);
    41.         gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = true;
    42.         gameObject.transform.GetChild(1).GetComponent<MeshRenderer>().enabled = true;
    43.     }
    44. }
    45.  
    Thank you!

    -CubeFlix
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    OnMouseDown only needs a collider on the object the script is attached to.
     
  3. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    It has a mesh collider, though.
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Does disabling the MeshRenderer also get rid of the mesh collider? If so that could be why you're unable to click on it after the model disappears, that's the only thing I can think of looking at this code. Maybe to fix this you need to keep the collider enabled somehow so you can still click on it without problems.

    Your code makes perfect sense, so I think that must be why this is happening.

    Edit: Sorry, just double checked your post, if you're dealing with a new asset you'll have to look at how the model is responding to you code but I'd still say the same it could be the collider is being taken away along with the mesh renderer if your code relies on the collider to still be there.
     
    Last edited: Mar 9, 2018
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,526
    Any errors in the console? Like maybe a NullReferenceError on one of those lines in OnMouseDown?

    Is something perhaps disabling TreeScript? It it still enabled when you click on the tree?
     
  6. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    No errors, and this TreeScript isn't the only one that is being affected. I have some rocks and animals that don't respond too.
     
  7. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    But, the code doesn't need to work any longer after the object's MeshRenderer is disabled, until the function Respawn is Invoked.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,526
    When you imported the legacy asset, did it overwrite your ProjectSettings? If it's an Asset Store asset in the Complete Projects or Templates category, these usually overwrite ProjectSettings.

    Have you tried reverting to a version of your project prior to importing that asset? (If you're not using version control, stop and read Creating Your First Source Control Repository.)
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Does the debug statement show up from the OnMouseDown method?
    If not, make an object with a collider, add a script and OnMouseDown with a debug and see if it works. Nothing else, just a debug statement.
    If that works, comment out your OnMouseDown on the tree, and write a new one with just a debug statement in it.
     
  10. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    Yes, it overrided
     
  11. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    I tried it on a different scene and it worked.
    It's just about the scene.
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Is something in that scene overlaying the screen area thus stealing the mouse event?

    In play mode, you could try deactivating other GameObjects in turn until you see the debug logging on mouse click. Then the last object deactivated could have been stealing the click events.
     
    CubeFlix likes this.
  13. CubeFlix

    CubeFlix

    Joined:
    Sep 9, 2017
    Posts:
    15
    I'll try that.