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;
}
}
}
} }
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
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!
no wait... you have to give a different name for each lamp, but you don't have to create different script
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.
Your answer
Follow this Question
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