- Home /
how do i fix this small error
I set the variable "hit" but is says I didn't so I dont know whats wrong with it.
public GameObject Cube;
void Update()
{
if(Input.GetKey(KeyCode.Mouse0))
{
RaycastHit Hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Cube.transform.position = (Hit.point);
}
}
error : error CS0165: Use of unassigned local variable `Hit'
Answer by robertbu · Jul 10, 2014 at 04:16 PM
There is nothing here that assigns a value to the 'hit' variable, so that is why you are getting the error. Usually there is a Raycast() in this code that assigns the value. Something like this inserted on line 10:
if (Physics.Raycast(ray, out hit) {
Cube.transform.position = hit.point;
}
The 'out' specifies that 'hit' will be changed by the function.
Answer by AyAMrau · Jul 10, 2014 at 04:19 PM
You create the variable but it's not initialised (you don't assign anything to it).
You also never run an actual raycast to check what the ray from the mouse input is hitting.
You should probably look here: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html and check the versions that use RaycastHit.
Your answer
Follow this Question
Related Questions
Raycasting ignoring small sized object 1 Answer
Getting the Furthest Point on a Ray 1 Answer
AI Pathfinding using RayCast 0 Answers
For each Raycast? 1 Answer
Setting a point with a ray C# 1 Answer