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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Rotate a Parent, click a child

Discussion in 'Scripting' started by filipetakanap, Jun 1, 2022.

  1. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Hey there, here i go again with another day wasted xD

    I have the following code:
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             Ray ray = Camera.main.ScreenPointToRay(this.gameObject.transform.position);
    6.             RaycastHit hit;
    7.             if (Physics.Raycast(ray, out hit))
    8.             {
    9.                 IncrementRotation(transform.parent);
    10.                 GameObject.Find("rotateCube(Clone)").transform.rotation =  Quaternion.Euler(0, 0, 0);
    11.  
    12.             }
    13.         }
    14.     }
    15.  
    16.     private void IncrementRotation(Transform xform)
    17.     {
    18.         if (xform != null) xform.rotation = m_incremental_rotation * xform.rotation;
    19.  
    20.     }
    That code is attached to a popup which is a cube with collision that appear when I click another object and fades after a while. The point is for the object to rotate, only when the popup is pressed, not when i click the big object:

    https://photos.app.goo.gl/AAXrbdfwfg2RDBcAA

    Thanks!
     
    Last edited: Jun 1, 2022
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Why don't you just check that
    hit
    is your popup? You didn't even check anything at all besides that your raycast hit something.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    More days are wasted in the world because of this type of coding:

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    Beyond that, you should never spin more than a very short while on a problem without making at least some kind of progress into understanding the next part of what is happening. Here's how to do that:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  4. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Thanks, i'll try to check it out !
     
  5. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    The GameObject.Find() was just a test i made to try to click only the popup.
    I'll try to figure out, thanks
     
    Kurt-Dekker likes this.
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    In addition to the other feedback in the thread, this is very wrong:

    Code (csharp):
    1. Ray ray = Camera.main.ScreenPointToRay(this.gameObject.transform.position);
    ScreenPointToRay takes a screen position, while you're giving it the world position of your object. You need to convert it using WorldToScreenPoint
     
  7. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I'm trying my best, but my brain is melting xD

    I've tryed: https://docs.unity3d.com/Manual/use-layers.html
    But i see nothing happening, not sure why

    I've tried creating a layer for just the popup and the Debug is returning 256 instead of 8:
    Code (CSharp):
    1.  
    2.  
    3. ...
    4.  int layerMask = (LayerMask.GetMask("Popup"));
    5. ...
    6.  
    7. void Update()
    8.     {
    9.         Debug.Log(LayerMask.GetMask("Popup"));
    10.      
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    14.             RaycastHit hit;
    15.          
    16.             if (Physics.Raycast(ray, out hit, layerMask))
    17.             {
    18.              
    19.                 IncrementRotation(transform.parent);
    20.             }
    21.         }
    22.     }
     
  8. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Another thing for me to figure out :p