Help me plz.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playercontroller : MonoBehaviour {
public UnityEngine.AI.NavMeshAgent agent;
void Awake()
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
public void MoveToLocation(Vector3 targetPoint)
{
targetPoint.x = transform.position.x + 20;
agent.SetDestination = targetPoint;
agent.isStopped = false;
}
}
So… what do you need help with?
I’m going to assume nothing is calling your MoveToLocation function.
line 21,49 error CS0120 Can anyone help fix this
using UnityEngine;
using UnityEngine.AI;
public class PlayerControler : MonoBehaviour
{
public NavMeshAgent agent;
public Camera cam;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
agent.SetDestination(RaycastHit.point);
}
}
}
}
That’s not even the same code. -_-
I really recommend you step back and (re)learn the basics. Have a look at the Roll-a-ball tutorial. It covers most of the basics, and it should give you a good understanding of both the code and Unity.
In either case, the latest error you’re seeing is because you’re using RaycastHit.point. You should be using hit instead.
1 Like
I really just wanna get this to work
Then you really need to learn to ask a decent question. Include at least (1) what you’re trying to do, (2) what you expect/want to happen, (3) what actually happens instead. Bonus points (which means more likelihood of getting good answers) if you can do all that using complete sentences. Yes, it matters.
EDIT: And the error shown there is because your script file name does not match the name of the MonoBehaviour class. That’s something explained in the beginner tutorials.
“pleasejustworkeee”…
I’m…speechless…
1 Like
First script, you were trying to assign to a function.
agent.SetDestination = targetPoint;
Should be
agent.SetDestination(targetPoint);
Second script, you called the file “pleasejustworkeee” (for some reason), but your class is called “PlayerControler”. They need to have the same name. Rename your script to “PlayerControler”.
The filename needs to be the same as the class name for MonoBehaviour derived classes.
Get into the habit of using leading uppercase for class names and leading lower case for variables and instances of your classes/objects. Variable types and their instances become clearer when you do this especially when code becomes complex.
1 Like