0
\$\begingroup\$

I have 3 Objects Capsule 1, Capsule 2 and Capsule 3. Every Capsule has a script on it for health/damage. It also contains an int for hits and points. Hits should be increased by one for every bullet that hits the target. Points if it gets destroyed (After 3 hits). These ints for points and hits should be displayed as a TextMesh. But how can I add 3 different ints to an Textmesh? Tried this code, but middle and right doesn't exist in context.

meshPoints.text = "Points: " + (points_right + points_middle + points_left) + "\nHits: " + hits_right;

How is that possible?

Using one script "health" on all of the objects doesn't work as well because after an object gets destroyed the ints become resetted...

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You should collect the 3 capsule objects like

GameObject[] capsules = GameObject.FindObjectsWithTag("capsule");

(perhaps you should add a tag to your gameobjects) and then your line will be

meshPoints.text = "Points: " + (capsules[0].GetComponent<ScriptAttached>().points_right + capsules[1].GetComponent<ScriptAttached>().points_middle + capsules[2].GetComponent<ScriptAttached>().points_left) + "\nHits: " + hits_right;
\$\endgroup\$
1
  • \$\begingroup\$ thanks, I created a class for every capsule with contains the hit and points int. Then I was able to access the ints from the other capsules as well. That was what I was looking for. I maybe try your solution later, too. \$\endgroup\$ Commented Mar 24, 2015 at 9:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.