///This is player script for player control
// this function for recording how many time I hit
var numbCoins : int = 0;
function OnTriggerEnter(GGG : Collider)
{
if(GGG.name == “Gate”)
{
numbCoins += 1;
guiPlayer.text = "Coins = " + numbCoins;
}
}
///this is Gate script for control an object, It will change by time increase.
function CheckCoins()
{
if(numbCoins <2 )
{
this.renderer.material = redMat;
}
if(numbCoins >=2 || numbCoins <4)
{
this.renderer.material = yellowMat;
}
if(numbCoins >= 4 || numbCoins <8 )
{
this.renderer.material = greenMat;
}
if(numbCoins >= 8 )
{
Destroy(gameObject);
}
}
/* I don’t know how to access the numb variable
for reference Gate script,
and there is one more script named HUD which linked Camera,
It contain Guitext for recording how much times ,
How dose these three script work?
Dose any one would tech me ? thank you!!!*/
A simple solution is to place references to the player script in Gate and HUD, and assign them in the Inspector. Supposing that the player script is called PlayerScript.js, for instance, the Gate script would be something like this:
public var pScript: PlayerScript; // drag the player here
function CheckCoins()
{
if(pScript.numbCoins <2 )
{
this.renderer.material = redMat;
}
else if (pScript.numbCoins <4)
{
this.renderer.material = yellowMat;
}
else if (pScript.numbCoins <8 )
{
this.renderer.material = greenMat;
}
else
{
Destroy(gameObject);
}
}
Place another reference in the HUD script and use it like in Gate:
public var pScript: PlayerScript; // drag the player here
function Update()
{
guiText.text = "Coins=" + pScript.numbCoins;
{
You can access a public variable from another script by this method.
This is your player script.
//PlayerScript.js
var numbCoins:int =0; //This is your public variable.
function OnTriggerEnter(GGG : Collider)
{
if(GGG.name == "Gate")
{
numbCoins += 1;
guiPlayer.text = "Coins = " + numbCoins;
}
}
This is your gate script (The script from which we access PlayerScript)
//GateScript.js
var playerobject:GameObject;
var playerCoins:int=Playerobject.GetComponent("PlayerScript").numbCoins;