Help with unity object selection and movement.
I'm trying to select and move objects with two classes. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class playermotor : MonoBehaviour {
NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
public void MoveToPoint (Vector3 point)
{
agent.SetDestination(point);
}
}
And this is the part that is supposed to do the actual moving.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class Commander : MonoBehaviour {
RaycastHit hit;
private List<GameObject> selectedunits;
public List<Vector3> Formations;
Camera cam;
playermotor motor;
//Fields for Ray
[SerializeField]
private LayerMask Clickable;
private LayerMask Terrain;
void Start(){
cam = Camera.main;
selectedunits = new List<GameObject> ();
}
//update is called once per frame
void Update (){
float panspeed = 10f;
Vector3 pos = transform.position;
//General Unit Selection and Movement
if (Input.GetMouseButtonDown (0)) {
Ray _ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(_ray, out hit, Mathf.Infinity, Clickable))
{
playercontroler clickonscript = hit.collider.GetComponent<playercontroler>();
if (Input.GetKey(KeyCode.LeftShift))
{
if (clickonscript.selected == false){
selectedunits.Add(hit.collider.gameObject);
clickonscript.selected = true;
clickonscript.clickme();
}
else
{
selectedunits.Remove(hit.collider.gameObject);
clickonscript.selected = false;
clickonscript.clickme();
}
}
else
{
if(selectedunits.Count > 0)
{
foreach (GameObject obj in selectedunits)
{
obj.GetComponent<playercontroler>().selected = false;
obj.GetComponent<playercontroler>().clickme();
}
selectedunits.Clear();
}
selectedunits.Add(hit.collider.gameObject);
clickonscript.selected = true;
clickonscript.clickme();
}
}
else
{
if(selectedunits.Count > 0)
{
foreach (GameObject obj in selectedunits)
{
obj.GetComponent<playercontroler>().selected = false;
obj.GetComponent<playercontroler>().clickme();
}
selectedunits.Clear();
}
}
}
if (Input.GetMouseButtonDown (1)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Debug.Log (hit.point);
foreach (GameObject obj in selectedunits)
{
if (obj.GetComponent<playercontroler>().selected) {
motor.MoveToPoint (hit.point);
}
}
}
}
Some big chunks of it are irrelevant, but I've included them just in case. If you catch any { mistakes, its probably not the source, I'm just bad with markdown. The last part is the part giving me trouble. It throws "Object referenced not set to an instance of and object" Why won't this activate the motor?
if (Input.GetMouseButtonDown (1)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Debug.Log (hit.point);
foreach (GameObject obj in selectedunits) {
if (obj.GetComponent<playercontroler> ().selected) {
motor.MoveToPoint (hit.point);
}
}
}
}
Your answer
Follow this Question
Related Questions
Cant Get Simple Boolean Statement to Work! 1 Answer
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer
Unity 4 error loading page 0 Answers
Button doesn't work on specific part of the game 0 Answers
WHY !! CountDown not Working inside the IF Conndition !!! 2 Answers