Variables return 0 even after assigning values.

Hello all,

I’m trying to get variables xID & yID of Sensor script on Sensor gameobject through another Cube script on Cube gameobject. Although Sensor script was able to convert the values and assign into xID & yID on trigger, but the xID & yID returns as 0 when called from Cube script.

I must have missed something. All help much appreciated. Cheerz!

Cube.cs

public int cubeXID, cubeYID;
public Sensor sensor;
    
void OnMouseDown(){
   sensor = GetComponentInChildren< Sensor >();
   if( sensor != null ){
     cubeXID = sensor.XID;
     cubeYID = sensor.YID;
   } else {
     Debug.Log ("missing sensor");
   }
}

Sensor.cs

public int xID, yID;

void OnTriggerEnter( Collider other ){
   if ( other.tag == "Ball" ){
     xID = (int) ( other.transform.position.x );
     yID = (int) ( other.transform.position.y );
   }
}

public int XID { get {return xID;}}
public int YID { get {return yID;}}

Are you sure sensor’s trigger event being fired ?
Are you sure your check for other.tag == “Ball” succeeds ?
Did you put a debug line just before line 5 of sensor.cs (inside if block) ?

Hint:

WHy does this…

cubeXID = sensor.XID;
cubeYID = sensor.YID;

Not look like this…

 xID = (int) ( other.transform.position.x );
 yID = (int) ( other.transform.position.y );

what do you suppose is the result of that?