Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vanidash-Studios · Apr 03, 2016 at 01:58 AM · functionlighttagonmousedowncalling

Tag calling is calling all my objects with the same tag

Hello, so i have some gameobjects (Lamps) who have the same tag name (Lights), and also i have a function that when i click on them, it turns off the component "Light" of those gameobjects, the thing is that when i click on one, all the lights are turned off in every lamp.

How can i call only 1gameobject the one im clicking to run the function to turnoff the light? *Also i want to execute this function only if my player is standing next to the gameobject im clicking on.

Thanks!

using System.Collections;

public class StreetLampFunction : MonoBehaviour {

 private Light myLight;
 void Start ()
 {
     myLight = GetComponent<Light>();
 }


 void Update()
 {
     if (Input.GetMouseButtonDown(1))
     {

         RaycastHit hitInfo = new RaycastHit();
         
         bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
         if (hit) 
         {
             if (hitInfo.transform.gameObject.tag == "Light")
                 //add code comparing vector position of the player and the vector position of the raycast hit position, if its next to it, execute the next line...


                 {
                 myLight.enabled = !myLight.enabled;
                 }

         } 
 }

} }

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

2 Replies

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

Answer by bromley · Apr 03, 2016 at 10:01 AM

it's only an idea, but you can use different names for each lamp, then you have to change the line

 if (hitInfo.transform.gameObject.tag == "Light")

with this new line

 if (hit.transform.name == name)

try this and tell me if it works

Comment
Add comment · Show 3 · 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
avatar image Vanidash-Studios · Apr 04, 2016 at 12:00 AM 1
Share

By doing that, it works fine, but its like an easy fix, because i will have a lot of "lamps" over my map, that means i will have a lot of scripts for every one of them. please reply if u come up with another solution.

Thanks!

avatar image bromley Vanidash-Studios · Apr 04, 2016 at 05:05 AM 0
Share

no wait... you have to give a different name for each lamp, but you don't have to create different script

avatar image Vanidash-Studios bromley · Apr 04, 2016 at 10:21 PM 0
Share

oooh, now i get it. thanks!

avatar image
0

Answer by Zetric · Apr 04, 2016 at 12:15 AM

Should be simple enough.

Seems like every light has its own raycast check from the camera to the mouse position. Since all lights fire a raycast check when you click, if you hit any of the lamps then, obviously all lamps would return a true value and fire their functions.

What you might want to do is to add a raycast script to your camera and then change the component of the transform you hit using something like:

 if(hit.transform.tag == "Light"){
      hit.transform.GetComponent("Light").enabled = true;
 }

This will make sure that 1: what you hit is a light. 2: that you only modify the component of the light you actually hit. Since you are only modifying the component of the transform that the raycast actually hit.

Note, this code is probably incorrect, its just to give you the idea.

Comment
Add comment · Show 1 · 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
avatar image Vanidash-Studios · Apr 04, 2016 at 10:22 PM 0
Share

Thanks, this is another valid option.

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

56 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

Related Questions

Find Script and call function FROM hit.transform 0 Answers

What is the recommend practice to check condition? Before calling a function or on the function? 0 Answers

Variable goes into the negative even if I reset it when it goes below 0. 1 Answer

OnPointerExit getting called when you click on a gameObject with the interface implemented. 0 Answers

How do I use arrays, with getcomponent? 2 Answers


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