Why doesnt my gameobject get filled?
I am trying to create a select object script in unity.
What it should do is when i hover over an object it colors red (and it does) and when i press "1" the GameObject targetSelected will be filled with the object i am hover on on that moment. In a Debug.Log this all works fine, the targetHighlighted is filled.
When i press "1" however the targetHighlighted object is empty. It doesn't matter if i press it still on the object or away from it.
Ofcourse the code was more extensive to use it the way i want. But in this code is the problem, so i reduced it to this.
Can anyone help me why when I press "1" the the Debug.Log doesn't show the targetHighlighted?
Basicly why do the mouseenter and mouseexit log the right Object, and the setTarget function doesnt?
Thanks in advance.
using UnityEngine;
using System.Collections;
public class TargetSelectionScript: MonoBehaviour {
GameObject targetHighlighted;
GameObject targetSelected;
Renderer rend;
Color initialColor = Color.white;
Color selectedColor = Color.red;
public GameControllerScript gameController;
void Start() {
}
void Update() {
if (Input.GetKeyDown("1")) {
SetTarget();
}
}
void OnMouseEnter() {
SelectTarget();
}
void OnMouseExit() {
ClearTarget();
}
void SelectTarget() {
RaycastHit hitInfo = new RaycastHit();
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
targetHighlighted = hitInfo.transform.gameObject;
rend = targetHighlighted.GetComponent < Renderer > ();
rend.material.color = selectedColor;
Debug.Log("Selected target: " + targetHighlighted);
}
void ClearTarget() {
Debug.Log(targetHighlighted);
}
void SetTarget() {
Debug.Log(targetHighlighted);
}
}
Hello! Sorry, I can't see when targetSelected
is assigned. TargetHighlighed
is assigned in line 34, and targetSelected
only checked in line 31 and no assingment in all the code. $$anonymous$$ay be you have posted only part of your code?
Hi, youre right about targetSelected. But as you can see in the code, i dont use it. I had much more code, but brought it down to this. The question is why targetHighlighted is filled via mouseenter and mouseexit, but not when pressed the "1" key.
Since i dont do anything with targetSelected, only declare it, targetSelected = null.
Answer by derooie · Sep 30, 2015 at 12:26 PM
Hi, thanks for youre reply. I was under the impression to use OnMouseEnter because it only is triggered once per object enter.
But if its better to use selecting the target via the Update function, I will give it a try tonight. Thanks.
PS: I still am very curious though, why my current code doesnt work ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612035518im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Text object updating on everything else except for variable 0 Answers
Activating an object after it's set as inactive? 0 Answers
How do you pick up an instantiated object? 1 Answer
Start moving unchecks itself 0 Answers
how to make an object change size automatically when playing so it doesnt go through objects 0 Answers