- Home /
Specific Items Being Clicked
Relative newbie, and I have a few questions, and one that's on the top of my head involves mouse inputs. I have a really simple scene, testing out a shuffle bag script I found on the forums here. In the scene there's a flat plane, a cube, and a sphere. I have this script attached to the cube…
#pragma strict
#pragma implicit
static class ShuffleBag{
var data = new Array();
var cursor=-1;
function add(item, num : int) {
var i = num || 1;
while(i--)
{
data.push(item);
}
cursor = data.length -1;
}
function next() {
var grab;
var temp;
if(cursor<1) {
cursor = data.length-1;
return data[0];
}
grab = Random.Range(0, cursor+1);
temp = data[grab];
data[grab]= data[cursor];
data[cursor] = temp;
cursor--;
return temp;
}
}
function Start()
{
}
var display : GUIText;
var displaylife = 1.0;
function Update()
{
if(Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)){
//startTime = Time.time;
//point = hit.point;
//object = hit.transform.gameObject;
ShuffleBag.add(0,1);
ShuffleBag.add(1,3);
print(ShuffleBag.next());
if(ShuffleBag.next() == 1){
var clonedisplay = Instantiate(display);
display.text = 'Hit!';
Destroy(clonedisplay, displaylife);
}
else if(ShuffleBag.next() == 0){
var clondedisplay = Instantiate(display);
display.text = 'Miss!';
Destroy(clonedisplay, displaylife);
}
}
}
else if (Input.GetMouseButtonUp(0)){
//duration = Time.time - startTime;
Destroy(clonedisplay, displaylife);
}
Destroy(clonedisplay, displaylife);
}
With a floating camera and when I point my camera at the cube, sphere, or plane, I get the same results from the if statements nested in Update(). How do I make it so it only the cube creates the GUItext display?
I know it's bad manner to ask two questions in one, but additionally, I'm curious why when the prefab for GUIText comes up, eventually the Hit! display won't be destroyed / removed and gets stuck on screen. Any idea how to better employ this display?
Thanks in advance!
$$anonymous$$y bad, just realized I have 'var' in front of the clonedisplay Instantiations, should remove those. Still that doesn't change the click complication…
Answer by riontamer · Dec 01, 2012 at 07:14 PM
solved my own question, combining things…
function Update(){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0)) {
if (Input.GetMouseButtonDown(0)){
ShuffleBag.add(0,1);
ShuffleBag.add(1,3);
RES = ShuffleBag.next();
print(ShuffleBag.next());
if(RES == 1){
clonedisplay = Instantiate(display);
clonedisplay.guiText.text = 'Hit!';
Destroy(clonedisplay, displaylife);
}
else if(RES == 0){
clonedisplay = Instantiate(display);
clonedisplay.guiText.text = 'Miss!';
Destroy(clonedisplay, displaylife);
}
}
}
}
The script checks to see if the POV of the character is pointing at the object, distance being 100 in this circumstance, but can be lengthened or decreased. THEN, instead of before, the if statement for Input.GetMouseButtonDown(0) comes into play. So, is the player looking at the object THEN is the mouse being clicked. Also fixed the display by having the global variables changed to…
var display : GameObject;
display.AddComponent("GUIText");
var displaylife = 1.0;
Essentially treating the text as a gameobject that has GUIText as a component rather than a GUIText object itself.
Finally (and even more unrelated to the topic, but for revisions sake) changed Shufflebag.next() to a global variable called RES so the outcome doesn't differ between the two if statements that are checking it. Kind of stupid on my part not to have had that before.
Hope this helps someone caught in the same spot of trouble as I was.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
trouble with instantiate 1 Answer
Player Input Action Map is being deleted when I import prefab to the scene 0 Answers
First Person Controller Erratic 1 Answer
Prefab Puzzle 1 Answer
Restrict touch input 0 Answers