The red part is the error, can anybody tell why it is happening. It says it doesn’t exist in current context.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Playercontroller : MonoBehaviour
{
public Camera cam;
private NavMeshAgent agent;
private void Start()
{
agent = GetComponent();
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
}
if (Physics.Raycast(ray, out hit))
{
agent.SetDestination(hit.point);
}
}
The title of your post is misleading, this just sounds like you’ve got a script compiler error and don’t know how to fix it.
If you have an error, post the full error. Also, post code using code-tags . This is a post for the scripting forum really.
You can clearly see in your code that you define the “hit” within the scope of the brackets in the previous “if () { xxxx }”. It doesn’t exist outside that scope. Why you’d put it there I have no idea.
“ray” and “hit” only exist within those brackets, it doesn’t make sense to do that.
f(Input.GetKeyDown(KeyCode.Mouse0))
{
...
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
}
Surely you want the raycast inside those brackets too.