Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DjamelEnidde · Jun 15, 2019 at 02:11 PM · touchclick objects

get event when i touch objects, "an animal for example"

i try to get animated voice when i touch my object (animal), i try this code, it run correctly when i use a cube object from unity, but when i try this code with 3D object that i downloaded , i dont get the voice. script.

//

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Sound : MonoBehaviour { public AudioClip[] aClips; public AudioSource MyAudio;

 string Animal;
  

 // Start is called before the first frame update
 void Start()
 {
     MyAudio = GetComponent<AudioSource>();
 }

 // Update is called once per frame
 void Update()
 {
     if((Input.touchCount > 0) && (Input.touches[0].phase == TouchPhase.Began))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit Hit;
         if(Physics.Raycast(ray, out Hit))
         {
             Animal = Hit.transform.name;
             switch (Animal)
             {
                 case "a1":
                     MyAudio.clip = aClips[0];
                     MyAudio.Play();
                     break;
                 case "a2":
                     MyAudio.clip = aClips[1];
                     MyAudio.Play();
                     break;
                 case "a3":
                     MyAudio.clip = aClips[2];
                     MyAudio.Play();
                     break;

                 /*case "Lion":
                     MyAudio.clip = aClips[0];
                     MyAudio.Play();
                     break;*/
                 default:
                     print("0000");
                     break;
             }
         }
     }
 }

}

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 Hellium · Jun 15, 2019 at 05:27 PM 0
Share

Do your animals have colliders (primitive or mesh colliders)?

avatar image Meishin · Jun 15, 2019 at 08:04 PM 0
Share

Hello @DjamelEnidde,

RayCast returns "bool True if the ray intersects with a Collider, otherwise false."

By default Unity Cubes are created with a cube collider. Did you check you have colliders attached to your gameobjects?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by camdenlink7 · Jun 17, 2019 at 02:30 PM

Make sure the object (or animal in this case) has a collider on it and that the names are indeed the same as the code.

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

142 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GuiTexture touch button problem 0 Answers

i need a script to make object be destoryed after touching another 1 Answer

GUI Texture Touch Android 2 Answers

Fixed force in direction of touch. 1 Answer

Touch and slide GUI box & grid selected buttons cSharp 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