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. Dismiss Notice

Question OnMouseDown() , is not changing variables declared outside.

Discussion in 'Scripting' started by aqeel25, Dec 29, 2021.

  1. aqeel25

    aqeel25

    Joined:
    Apr 8, 2019
    Posts:
    25
    I am using
    OnMouseDown()
    like following
    Code (CSharp):
    1.  public class Rotateprents : MonoBehaviour
    2. {  
    3.       GameObject P1;
    4.       GameObject C1;
    5.       GameObject C2;
    6.   void OnMouseDown()
    7.      {
    8.          if (transform.name.Contains("Parent"))
    9.          {
    10.              P1 = transform.gameObject;
    11.              C1 = P1.transform.GetChild(0).gameObject;
    12.              C2 = P1.transform.GetChild(P1.transform.childCount - 1).gameObject;
    13.              Debug.Log(C1);
    14.          }
    15.          else if (transform.name.Contains("child"))
    16.          {
    17.              P1 = transform.parent.gameObject;
    18.              C1 = P1.transform.GetChild(0).gameObject;
    19.              C2 = P1.transform.GetChild(P1.transform.childCount - 1).gameObject;
    20.          }
    21.        
    22.      }
    23.   public void rotateobj()
    24.      {
    25.         Debug.Log(C1);
    26.       }
    27. }
    Gameobject C1 is empty in rotateobj(). Why?
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Maybe Debug.Log(C1.name) instead to see what C1 became in both conditionals. Give some sort of mark for each log to tell when and where you are seeing them; Debug.Log("1: " + C1.name).