How to display different objects on touch

Hi all, I’ve hit a brick wall with what I’m trying to do at the moment! Can anyone please help me?

In my scene I have a character “mole commander” When I click on him for the first time I have it displaying a block of text (texture map on an object) that instantiates to the side of him onto a holder object.

What I am trying to do is figure out how to change this text on the second click (at the moment it instantiates the same item on each click)

I’m thinking I need an array? and somehow cycle through this on each new click but I have no idea how to code an array or how I could cycle through it on each click.

Or is it possible to code for 1st raycast hit, 2nd raycast hit etc?

my goal is: on first click “1st piece of text” on second click “2nd piece of text” and so on

can anyone help me please?

this is my code so far:

var speech_bubble_holder : GameObject;
    var speech_bubble : GameObject;
    private var ray : Ray;
    private var rayCastHit : RaycastHit;
    var mySound: AudioClip;
    var bloodPrefab : GameObject;
    
    function Update(){
    
    if(Input.GetMouseButtonDown(0)){
    
    ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    
    if (Physics.Raycast (ray, rayCastHit)){
    
    //add name of object/button to affect in ""
    if(rayCastHit.transform.name == "mole_commander_interigate"){
    
    audio.clip = mySound; 
    audio.PlayOneShot(mySound);
    //audio.Play();
    
    	var speech_bubble : GameObject = Instantiate(speech_bubble, speech_bubble_holder.transform.position, speech_bubble_holder.transform.rotation);
    	speech_bubble.transform.parent = speech_bubble_holder.transform;
    
    Debug.Log("interigating");
    Scorecounter.Counter +=50;
    //Scorecounter.Counter ++;
    
    var rot = Quaternion.FromToRotation(Vector3.up, rayCastHit.normal);
    Instantiate(bloodPrefab, rayCastHit.point, rot);
    
    //Destroy(rayCastHit.collider.gameObject);
    
    Debug.Log ("interigating commander mole!");
    }
    }
    }
    }

You shouldn't use Quaternion.FromToRotation in such a case. Use Quaternion.LookRotation(pos-center)

1 Answer

1

I don’t see anywhere where you have more than one speech bubble.

Since you appear to be creating them as world geometry here:

var speech_bubble : GameObject;

You’ll want to create as array of these objects for your various speeches. Then you can assign each in the inspector.

Once all are assigned, be sure to keep a variable for which bubble was selected last.

In the Start() method, set it to something like -1. Each time the click happens, increment by one and use modulus to wrap around.

currentIndex = (currentIndex + 1) % speech_bubble.length;

And show the correct speech bubble in the Instantiate call by referencing this index:

Instantiate(speech_bubble[currentIndex], speech_bubble_holder.transform.position, speech_bubble_holder.transform.rotation);

Great, thanks!