Hello,
I have a prefab which I am using in the game as the enemy. Now - I have a variable attach to that which transforms when it gets a hit. This variable is accessed by an empty gameobject which is responsible to display it using OnGUI() function.
Code:
var r_enepos : float;
var e_dis : float;
var e_distance : float;
function OnCollisionEnter(hit : Collision)
{
randomrange();
if(hit.gameObject.tag == "cannonball")
{
transform.position = Vector3(r_enepos,2,0);
}
}
function Update()
{
e_distance = gameObject.transform.position.x + 21;
Debug.Log("enemy distance" + e_distance);
if(gameObject.transform.position.z!=0)
gameObject.transform.position.z=0;
}
function randomrange()
{
if(Random.Range(0,10)<7)
r_enepos = Random.Range(-18,10);
else
r_enepos = Random.Range(10,21);
}
Code Explanation:
If you have to look into the code, e_distance is the variable which is inherited in another emptygameobject in the OnGUI() function.
GameObjects:
This script is attached to the “enemy” prefab and that prefab is drag onto the empty gameobject which is referencing this script. To be clear again one is a Prefab and other is an gameobject on the scene view.
Problem:
As you might see on the code, I make an attempt to print it on the screen. When I run the level, the value that prints on the console is accurate but on the GUI , it is just zero (which is the initial value). Moreover when I did trace a bit - I happen to find that the variable e_distance on the inspecter pane of “enemy” prefab remains zero. i.e. the value in the prefab does not change.
Request:
I am not sure where I have went wrong. I would be happy and would appreciate if someone could help me on this.
THANK YOU in advance.