- Home /
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.
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.
@Dreamblur, I believe I suggested both of those exact ideas in my 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();
}
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
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
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
Then put them in an array when there destroyed:
var list = new int[10];
OnTriggerEnter () {
var number = .......
list[number -1] = number;
}
Your answer
Follow this Question
Related Questions
OnTriggerExit() help 0 Answers
i need detailed instructions... cue texture 1 Answer
on trigger enter 1 Answer
OnTriggerEnter problem 3 Answers
OnTriggerEnter calling infinitely 1 Answer