x


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?

more ▼

asked May 19 '11 at 07:48 PM

Inan Evin gravatar image

Inan Evin
181 38 60 64

@macfanpro , thank you it works :)

May 19 '11 at 08:09 PM Inan Evin
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered May 19 '11 at 07:55 PM

ckfinite gravatar image

ckfinite
1.3k 5 9 24

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x133
x50

asked: May 19 '11 at 07:48 PM

Seen: 432 times

Last Updated: May 19 '11 at 08:09 PM