Ich habe ein sehr verwirrendes Problem mit einem Null-Referenzfehler. Es liest sich wie:Merkwürdiger Null-Referenzfehler beim Hinzufügen eines Elements zu IList
NullReferenceException: Object reference not set to an instance of an object
GameHandler.Update() (at Assets/Scripts/GameHandler.cs:33)
Hier ist der lange Kontext:
C#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameHandler : MonoBehaviour {
public GameObject canvas;
IList<GameObject> selected;
GameObject[] troopObjects;
bool isSelecting = false;
Vector3 mousePosition1;
void Update() {
// If we press the left mouse button, save mouse location and begin selection
if (Input.GetMouseButtonDown(0)) {
isSelecting = true;
mousePosition1 = Input.mousePosition;
if(selected != null) {
foreach (GameObject selectedTroop in selected) {
selectedTroop.transform.GetChild(0).gameObject.SetActive(false);
};
};
selected = null;
};
// If we let go of the left mouse button, end selection
if (Input.GetMouseButtonUp(0)) {
isSelecting = false;
};
if (isSelecting) {
troopObjects = GameObject.FindGameObjectsWithTag("Troop");
foreach (GameObject troop in troopObjects) {
if (IsWithinSelectionBounds(troop)) {
selected.Add(troop);
troop.transform.GetChild(0).gameObject.SetActive(true);
};
};
};
if (selected != null) {
};
}
// Use this for initialization
void Start() {
canvas.SetActive(false);
}
void OnGUI() {
if (isSelecting) {
// Create a rect from both mouse positions
var rect = Utils.GetScreenRect(mousePosition1, Input.mousePosition);
Utils.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
Utils.DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
}
}
public bool IsWithinSelectionBounds(GameObject gameObject) {
if (!isSelecting)
return false;
var camera = Camera.main;
var viewportBounds = Utils.GetViewportBounds(camera, mousePosition1, Input.mousePosition);
return viewportBounds.Contains(camera.WorldToViewportPoint(gameObject.transform.position));
}
}
Nun ist die Codeproblem erscheinen würde dieses Recht hier zu sein:
if (isSelecting) {
troopObjects = GameObject.FindGameObjectsWithTag("Troop");
foreach (GameObject troop in troopObjects) {
if (IsWithinSelectionBounds(troop)) {
selected.Add(troop);
troop.transform.GetChild(0).gameObject.SetActive(true);
};
};
};
Was die Fehler Referenziert ist dies:
selected.Add(troop);
Es sagt, dass "Truppe" eine Nullreferenz ist. Jetzt, wenn ich diese Codezeile entferne, funktioniert der Rest gut. Was keinen Sinn macht, denn gleich nach diesem Problemcode gibt es das:
troop.transform.GetChild(0).gameObject.SetActive(true);
Welches verwendet die gleiche "Truppe" Verweis. Ich würde mich für etwas Hilfe bei diesem ein, weil es mich ratlos ist. Wenn zusätzliche Informationen benötigt werden, lassen Sie es mich wissen.
Umm, ich bin ziemlich sicher, dass es 'ist ausgewählt 'das ist' null', nicht 'truppe'. Es ist nicht so, als würde ein "Null" -Wert zu einer Liste hinzugefügt werden, ist verboten ... In der Tat, du füllst nie 'ausgewählt' und du setzt es explizit auf' null' :) – Luaan
troop ist nicht null, die Liste ist ! – Ash