Unable to correctly use game objects?
I have two problems that I'm not sure about why they're not working.
First, which I just implemented another a different way because I couldn't get it to work, was that when I accessed the Light component of a child game object, I was unable to enable or disable it. However, I was able to enable and disable the light when I set the light in the inspector.
Second, when I try to compare two game objects and I know they're the same, it never evaluates them to be the same. I've tried == by game objects, ReferenceEquals, == by instance ID, and none have worked.
Working function that turns off lights using inspector.
private void TurnOffAllButtonLights()
{
for (int i = 0; i < buttonLights.Length; i++)
{
buttonLights[i].enabled = false;
}
}
Non-working function to turn off lights. GetPlayButtons() returns the list of play buttons from a different script.
private void TurnOffAllButtonLights()
{
for (int i = 0; i < buttonSystem.GetPlayButtons().Length; i++)
{
buttonSystem.GetPlayButtons()[i].GetComponentInChildren<Light>().enabled = false;
}
}
Function that gets player input and checks if they clicked one of the play buttons.
private void ProcessPlayerInput()
{
bool isGameButton = false;
GameObject[] playButtons = buttonSystem.GetPlayButtons();
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
bool hasHit = Physics.Raycast(ray, out hit);
for (int i = 0; i < playButtons.Length; i++)
{
if (playButtons[i].gameObject == hit.transform.gameObject)
{
Debug.Log("Is Game Button");
isGameButton = true;
}
}
if (hasHit && isGameButton)
{
Debug.Log(hit.transform.gameObject.name);
}
}
I've printed out the name of the game object and it says they're the same, but they're still not evaluated to be the same. Is it because it's referencing it from another script? Or something else I'm missing?
I'd appreciate any help.
Answer by TheonlysiQ · Sep 19, 2020 at 09:10 PM
Only commenting on the second issue. First of all i can validate ReferenceEquals is the way to go when comparing gameobjects. This works. If it doesn't in your case, i have doubts that the objects you are comparing are actually the same. Did you actually use Debug.Log() and saw that the instance ids are the same? Can you double check this?
Looked at the snippet briefly and i got a bit confused because it seems you are accessing a gameObject property on the playButtons[i] (which is already a gameobject??), when doing the comparison. That seems odd. Try doing it with just playbuttons[i].
Also raycasthit.transform returns the transform of the rigidbody/collider that was hit - https://docs.unity3d.com/ScriptReference/RaycastHit-transform.html
So perhaps you could also verify that the game object which is hit actually has a rigidbody/collider attached.
PS Post just one issue per ticket, otherwise I believe you dramatically lower your chances of getting responses (unless tryhards like me stumble upon your post).
Answer by Bunny83 · Sep 19, 2020 at 10:29 PM
Well, it depends entirely on what your GetPlayButtons method actually does. Where is that list of gameobjects coming from? Also are you sure that each of those gameobjects only has one light component ? Keep in mind that GetComponentsInChildren first searches on the gameobject itself before it seaches on children.
Depending on how you actually obtain that list of gameobjects it's possible that you get them from the wrong object. A common mistake is to execute a method on a prefab and not on the actual instance in the scene. Try executing:
GameObject[] playButtons = buttonSystem.GetPlayButtons();
for (int i = 0; i < playButtons.Length; i++)
{
Debug.Log("go: " + playButtons[i].name, playButtons[i]);
}
Note the second parameter to Debug.Log. The context object will be highlighted in the Unity editor when you click on the debug message in the console. That helps to find the object, either in the hierarchy or on the project (in case it's an object on a prefab).
Your answer
Follow this Question
Related Questions
How to get a custom component declared by variable? 1 Answer
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer
how to get name of game object with position by raycast 1 Answer
No gameobject instance again in update 1 Answer
Adding created component to game object. 0 Answers