- Home /
Problem About Gaining Points
Hello everyone, i have 107 golds in my scene and we need to collect them, i wrote a code but it takes the first one, but it doesn't take the second one, what can be the problem ?
Here it's :
var distance = 100;
var gold : Transform;
var blipsound : GameObject;
var gold2 : Transform;
function Update ()
{
GameObject.Find("g_Points").guiText.text = ""+PointCode.points;
if(Vector3.Distance(transform.position,gold.position) < distance)
{
GainPoints();
}
else if (Vector3.Distance(transform.position,gold2.position) < distance)
{
GainPoints();
}
}
function GainPoints()
{
Destroy(gold.gameObject);
PointCode.points+= 5;
blipsound.audio.Play();
}
what should i do ? If there was 10 or 15 golds, i could put 15 different scripts which are specific for each gold . But there are 107 golds, so when i did it the FPS reduces to 2 , any suggestions to make that script work?
Answer by ckfinite · May 19, 2011 at 07:55 PM
Basically, what you are doing is hard coding the distance check for all of the gold objects, drastically reducing flexibility. There is one good way to do what you want to do, and it is pretty simple.
First, you need to add a BoundingSphere to your gold prefab (Components->Physics->Bounding Sphere), set the sphere to the size you want, and check is trigger on the sphere. Then, add a new behavior to the gold object. The behavior should contain the following:
function OnTriggerEnter(coll : Collider)
{
blipsound.audio.Play();
PointCode.points+= 5;
Destroy(gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612081026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Non stopping point - counter 1 Answer
Point system, something wrong 1 Answer
points system with coins 3 Answers
How to make a character collect items? 4 Answers
Custom Sensor integration 0 Answers