• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by robert_mathew · Jul 30, 2011 at 04:01 PM · triggeronenter

fetching a value

I have ten ball hanging in air i have tagged all balls as ball and a cube in ground for on trigger enter .when any ball hit the cube it get destroy.The trigger part is working fine for me. My need is i have to get a numeric value according to the ball get hitted on the cube.for example if ball1 is collided with the cube i want to display numeric 1 in Gui like wise if ball2 got collided i want to display 2 on the guy if ball 10 gui should be numeric 10 like wise.I have attached the collision script on cube placed in ground.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamblur · Jul 30, 2011 at 09:11 PM 0
Share

Store the numeric value in the object's name (either as the actual name or a part of it) and parse it afterwards. That, or include a script to each object containing the numeric value it corresponds to individually. The second way is too "brute force"-ish to me, but it's the best way for a beginner.

avatar image Peter G · Jul 30, 2011 at 09:22 PM 0
Share

@Dreamblur, I believe I suggested both of those exact ideas in my answer. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Peter G · Jul 30, 2011 at 04:42 PM

There are a few ways you could go about this. If you have a consistent naming convention, you could split the string after the "ball" and just read the number.

  //given balls' names are "ball1", "ball2" and so fourth.
  //ballCollider is the collider passed into OnTriggerEnter
  var number : String = ballCollider.gameObject.name;
  number = number.Remove(0, 4);
  guiText.text = number;
 
  //if you want to parse the number...
  var numberInt : int = Int32.Parse(number);
  //now you have an integer representation of it.

or you could add a script to each ball that stores a value:

 //BallScript.js
  var value : int;

and then the trigger:

  OnTriggerEnter (ballCollider : Collider) {
       guiText.text = ballCollider.GetComponent.<BallScript>().value.ToString();
  }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robert_mathew · Jul 30, 2011 at 06:45 PM 0
Share

your concept is working due to ball get destroyed on trigger enter i cannot able to use the numeric value for further in game for some other calculation i want to use the ball values further in my game

avatar image Peter G · Jul 30, 2011 at 07:34 PM 0
Share

I don't understand the second part of your comment.

avatar image robert_mathew · Jul 31, 2011 at 01:52 AM 0
Share

i am adding script to each ball and i am using the value i get from collision for score calculation when collision happens the i get a value from the ball due to ball get destroying as script is inside the ball the script is also get destroying along with the ball so i cannot able to access the numeric value that i get according to the ball which collided for further score calculation

avatar image robert_mathew · Jul 31, 2011 at 01:52 AM 0
Share

i am adding script to each ball and i am using the value i get from collision for score calculation when collision happens the i get a value from the ball due to ball get destroying as script is inside the ball the script is also get destroying along with the ball so i cannot able to access the numeric value that i get according to the ball which collided for further score calculation

avatar image Peter G · Jul 31, 2011 at 03:43 AM 0
Share

Then put them in an array when there destroyed:

  var list = new int[10];
  
  OnTriggerEnter () {
     var number = .......
     list[number -1] = number;
  }

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

i need detailed instructions... cue texture 1 Answer

on trigger enter 1 Answer

OnTriggerEnter problem 3 Answers

OnTriggerExit() help 0 Answers

How can i detect by script when a character enter a hole in object ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges