Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

RTS Scripting - Need help

Discussion in 'Scripting' started by Xeno__, Feb 11, 2020.

  1. Xeno__

    Xeno__

    Joined:
    Oct 7, 2015
    Posts:
    5
    Hey,
    I've been working on a small RTS project as my first game and I've gotten stuck on this bug for a while and not sure how to get around it since I'm new to the language and don't have too much experience.
    Would be great if someone could point out the issue and tell me what I should do.

    Currently, I'm trying to make a unit move to the position of my mouse for all units that are selected and am receiving an error saying NullReferenceException: Object reference not set to an instance of an object.

    Here are my relevant scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Actions : MonoBehaviour
    6. {
    7.     //private Vector3 target;
    8.     //public float speed = 20f;
    9.     //public Vector3 startMarker; //Start position of journey
    10.     //public Vector3 endMarker; //End position of journey
    11.     //private Vector3 pos; //Position of mouse
    12.     //private float startTime;
    13.     //private float journeyLength;
    14.     public LayerMask hitLayers;
    15.     UnitManager unitManager;
    16.  
    17.     void Update()
    18.     {
    19.         Vector3 mouse = Input.mousePosition;
    20.         Ray castPoint = Camera.main.ScreenPointToRay(mouse);
    21.         RaycastHit hit;
    22.         if (Physics.Raycast(castPoint, out hit))
    23.         {
    24.             if (Input.GetMouseButtonDown(1))
    25.                 unitManager = GetComponent<UnitManager>();
    26.                 for (int i = 0; i < unitManager.selectedUnits.Count; i++)
    27.                 {
    28.                     unitManager.selectedUnits[i].transform.position = hit.point;
    29.                     print("moved");
    30.                 }
    31.         }
    32.     }
    33. }

    Script I'm trying to access the list from:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class UnitManager : MonoBehaviour
    7. {
    8.     RaycastHit hit;
    9.     public List<Transform> selectedUnits = new List<Transform>();
    10.     bool isDragging = false;
    11.     Vector3 mousePosition;
    12.     private bool isMultiSelect;
    13.  
    14.     private void OnGUI()
    15.     {
    16.         if (isDragging)
    17.         {
    18.             var rect = ScreenHelper.GetScreenRect(mousePosition, Input.mousePosition);
    19.             ScreenHelper.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.1f));
    20.             ScreenHelper.DrawScreenRectBorder(rect, 1, Color.blue);
    21.         }
    22.  
    23.     }
    24.  
    25.  
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         //Detect if mouse is down
    31.         if (Input.GetMouseButtonDown(0))
    32.         {
    33.             mousePosition = Input.mousePosition;
    34.             //Create a ray from the camera to our space
    35.             Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    36.             //Shoot that ray and get the hit data
    37.             if (Physics.Raycast(camRay, out hit))
    38.             {
    39.                 //Do something with the data
    40.                 if (hit.transform.CompareTag("PlayerUnit"))
    41.                 {
    42.                     SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
    43.                     Debug.Log(hit.transform);
    44.                 }
    45.                 else if (hit.transform.CompareTag("Ground"))
    46.                 {
    47.                     DeselectUnits();
    48.                 }
    49.             }
    50.         }
    51.  
    52.     }
    53.  
    54.     private void SelectUnit(Transform unit, bool isMultiSelect)
    55.     {
    56.         if(!isMultiSelect)
    57.             {
    58.                 DeselectUnits();
    59.             }
    60.         selectedUnits.Add(unit);
    61.         unit.Find("Highlight").gameObject.SetActive(true);
    62.     }
    63.  
    64.     private void DeselectUnits()
    65.     {
    66.         for(int i = 0; i < selectedUnits.Count; i++)
    67.         {
    68.             selectedUnits[i].Find("Highlight").gameObject.SetActive(false);
    69.         }
    70.         selectedUnits.Clear();
    71.     }
    72. }
    73.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    What's the exact line where the error occurs?

    A Null Reference Exception basically means you put a dot (.) after the name of some variable that doesn't currently have a value assigned to it.

    For example, this might happen if you tried to GetComponent<Something> and then do something with that component, but it turned out that your object didn't have any Something attached to it.
     
  3. Xeno__

    Xeno__

    Joined:
    Oct 7, 2015
    Posts:
    5
    The error appears on line 26 of the Actions script
    The error appears on line 26 of the Actions script
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Then either unitManager is null (meaning there is no UnitManager component attached to the same object as your Actions component) or unitManager.selectedUnits is null (I don't immediately see a way this would happen, so my money is on the first thing).

    If you're not sure which, you can try adding Debug.Log statements to test them each one at a time.
     
  5. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    Hello , i need to help of my first RTS I have this error... Does it tje solution of this error?
    Assets \ Scenes \ Script camera \ InputManager.cs (12,1): erreur CS1519: jeton non valide '{' dans la déclaration de membre de classe, de structure ou d'interface