- Home /
How can I enable my capsule to move; object 'target' not recognized.
Hello there everyone. I'm new to unity and am trying to work through my first tutorial. Unfortunately, I'm having issues with the initial script being able to run. I was able to run this script on the first machine (Machine A), but unable to successfully run the script on the second (Machine B). The only difference when executing and attaching to unity is on Machine B I receive an error message "Field 'Mover.target' is never assigned to, and will always have its default value null." This does not appear on machine A. I'm not sure if the issues are related, but another discrepancy is System.Collection & System.Collections.Generic are also not utilized on Machine B.
The script I'm attempting is below. There is an object called 'target' within the scene. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class Mover : MonoBehaviour { [SerializeField] Transform target;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
MoveToCursor();
}
}
private void MoveToCursor()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool hasHit = Physics.Raycast(ray, out hit);
if (hasHit)
{
GetComponent<NavMeshAgent>().destination = hit.point;
}
}
}
Note: I also attempted to run a more simple script to move the capsule to the target, and was unsuccessful on Machine B. This script is receiving the same error. Script below, using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Mover : MonoBehaviour { [SerializeField] Transform target;
private void Update()
{
GetComponent<NavMeshAgent>().destination = target.position;
}
}
Please help, I've tried several different methods of movement on this Machine, and have not been successful. This has really stopped me in my tracks for furthering any tutorial.,Hello everyone,
Hey Quackpants25 ! Your are getting error becuase you define a variable in your $$anonymous$$overScript Called Target! Target Is Filled In $$anonymous$$achine A but in $$anonymous$$achine B Its Not Assigned So Fill It with your target so it cant be null anymore! a good tip is always check your reference in this way if(target!=null) // make sure it is not null { //dothis }
Thank you very much for your response @Fariborzzn! That absolutely makes sense, I'm just not sure how to execute & move forward. When you say fill it with your target, how would I do that? How do I get the target assigned in $$anonymous$$achine B? I thought I had been doing this by making the same objects and using the same scripts on $$anonymous$$achine A & B. Is this something that would be done to my object 'target' within the scene?
I'll play around with this information and attempt to google ways to implement in the meantime. Thanks again!
Your Welcome Dear! [SerializeField] Transform target; // This is Filled You Had Define Click On the gameobject you can find it in $$anonymous$$over Component..
You Shoud Drag and Drop Your Target Object From Hierarchy To It
Your answer
Follow this Question
Related Questions
HOW TO MAKE A NPC GO FORWAR 0 Answers
How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer
How to make the sprite move a certain amount of pixels when a key is pressed? 1 Answer
MoveTowards is moveing my object to random position when i click it is already on a way 1 Answer