Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 carter-carl30 · Apr 08, 2013 at 10:39 PM · instantiatearraysraycasthitcycle

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!");
     }
     }
     }
     }
Comment
Add comment
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

1 Reply

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

Answer by Dracorat · Apr 08, 2013 at 10:55 PM

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);
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

How can I instantiate an explosion using Raycasting? 0 Answers

Instantiating prefab with certain coordinates problem (c#) 0 Answers

Three Random Objects From Array Without Repeat 1 Answer

Projectile Selection through Array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges