Hi,
I would like the character get one score when collide with a gold cube. Just like the game example : Penelope.
private var gotGold = false;
static var SCORES = 0;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "gold")
{
gotGold = true;
SCORES += 1;
Debug.Log("Score : " + SCORES);
}
}
function LateUpdate ()
{
if(gotGold)
{
if(SCORES == 5)
{
//Won Game
transform.position = Vector3(0,0,0);
gameObject.Find("Camera Relative Controls").transform.position = Vector3(0,0,0);
}
gotGold = false;
}
}
In Gold.js
function KillGold()
{
goldPool.Recycle(this);
}
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "Character")
{
KillGold();
}
}
-
When character hit the gold cube, the number of score keep increase: 1,2,3,4,5,6,…
-
The gold cube does not disappear and still show on screen.
-
How can the SCORE be displayed on screen?
What does goldPool.Recycle(this); do exactly?
I understand the concept of a pool, but I mean does this implementation deactivate the gameobject, or does it just add it to an array(list)?
Here is the Recycle script:
public function Recycle(thisGold : Gold)
{
thisGold.transform.position = thisTransform.position;
thisGold.gameObject.active = false;
}
However, it has error:
The object of type ‘Gold’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
So where do you destroy it?
If I add the code
Destroy(thisGold);
in Recycle function.
There would be reducing the number of Gold.
However, I want to keep on re-using the preloaded Gold object on screen. So, just set it inactive.
I think I am wrong here and I need advice.
If I destroy it, but the object is instantiated on Start().
Please take a look of the complete code at
http://forum.unity3d.com/viewtopic.php?t=56525
I change it for the Gold object array.
function Start ()
{
thisTransform = transform;
goldArray = new Gold[goldCache];
for (var index = 0; index < goldCache; index++) {
newGold = Instantiate(goldPrefab, Vector3(Random.Range(-100, 160), 10, Random.Range(-160, 100)), Quaternion.identity);
goldArray[index] = newGold.GetComponent(Gold);
newGold.active = false;
}
}
What is your idea?
By the way, I use the code to do the score display:
function OnGUI ()
{
GUI.Label(Rect(400, 5, 200, 20), "Score: " + playerScore + "/" + scoreToWin);
}
[/code]