C
C#2y ago
white wine

❔ Object reference not working (unity)

Hello i am somewhat new to coding and am having an issue in my code and i cant identify it so if you could help that would be appreciated!
21 Replies
white wine
white wine2y ago
if you have any questions about my script feel free to ask
lycian
lycian2y ago
can you post the code somewhere that isn't a screenshot? I'm old and can't read that 😦
white wine
white wine2y ago
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Game : MonoBehaviour { public GameObject chesspiece; private GameObject[,] positions = new GameObject[8, 8]; private GameObject[] playerBlack = new GameObject[16]; private GameObject[] playerWhite = new GameObject[16]; private string currentPlayer = "White"; private bool gameOver = false; // Start is called before the first frame update void Start() { playerWhite = new GameObject[] { Create("white_rook", 0, 0), Create("white_knight", 1, 0), Create("white_bishop", 2, 0), Create("white_queen", 3, 0), Create("white_king", 4, 0), Create("white_bishop", 5, 0), Create("white_knight", 6, 0), Create("white_rook", 7, 0), Create("white_pawn", 0, 1), Create("white_pawn", 1, 1), Create("white_pawn", 2, 1), Create("white_pawn", 3, 1), Create("white_pawn", 4, 1), Create("white_pawn", 5, 1), Create("white_pawn", 6, 1), Create("white_pawn", 7, 1), }; playerWhite = new GameObject[] { Create("black_rook", 7, 7), Create("black_knight", 6, 7), Create("black_bishop", 5, 7), Create("black_queen", 4, 7), Create("black_king", 3, 7), Create("black_bishop", 2, 7), Create("black_knight", 1, 7), Create("black_rook", 0, 7), Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6), Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6), Create("black_pawn", 6, 6), Create("black_pawn", 7, 6), }; for (int i = 0; i < playerBlack.Length; i++) { SetPosition(playerBlack[i]); SetPosition(playerWhite[i]); } } public GameObject Create(string name, int x, int y) { GameObject obj = Instantiate(chesspiece, new Vector3(0, 0, -1), Quaternion.identity); Chessman cm = obj.GetComponent<Chessman>(); cm.name = name; cm.SetXBoard(x); cm.SetYBoard(y); cm.Activate(); return obj; } public void SetPosition(GameObject obj) { Chessman cm = obj.GetComponent<Chessman>(); positions[cm.GetXBoard(), cm.GetYBoard()] = obj; } public void SetPositionEmpty(int x, int y) { positions[x, y] = null; } public GameObject GetPosition(int x, int y) { return positions[x, y]; } public bool PositionOnBoard(int x, int y) { if (x < 0 y > 0 x >= positions.GetLength(0) || y >= positions.GetLength(1)) return false; return true; } }
phaseshift
phaseshift2y ago
You never create any players Well you do, but in guessing you're using those arrays before there's anything in them Doesn't the error tell your what line the problem is on?
white wine
white wine2y ago
i have my other script chessman which created the black and white player its apparently on line 49 or " public void SetPosition(GameObject obj) { Chessman cm = obj.GetComponent<Chessman>(); positions[cm.GetXBoard(), cm.GetYBoard()] = obj; }"
phaseshift
phaseshift2y ago
And what's the error? obj is null?
white wine
white wine2y ago
yeah it saying object reference not set to an instance of an object
phaseshift
phaseshift2y ago
Yeah, you don't have any Attach a debugger It's ab simple mistake
white wine
white wine2y ago
i tried attaching a debugger and im getting another error saying i need to use a preview version for a line of code used by the debugger
phaseshift
phaseshift2y ago
Are you on a dodgy copy of Windows?
white wine
white wine2y ago
No
phaseshift
phaseshift2y ago
Why isn't it activated then?
white wine
white wine2y ago
Oh if thats what you ment yeah
phaseshift
phaseshift2y ago
Sorry, we can't help you here
white wine
white wine2y ago
Oh alright
Playboi17
Playboi172y ago
What’re you talking about, why are you being rude to him for not having an activated windows license? Anyone can use windows 10/11 for free without breaking TOS If you want bonus features, you pay for a license. Illegal would be activating your license without paying
lycian
lycian2y ago
@Forever#5620 when pasting code try to use markdown
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour
{
public GameObject chesspiece;

private GameObject[,] positions = new GameObject[8, 8];
private GameObject[] playerBlack = new GameObject[16];
private GameObject[] playerWhite = new GameObject[16];

private string currentPlayer = "White";

private bool gameOver = false;

// Start is called before the first frame update
void Start()
{
playerWhite = new GameObject[]
{ Create("white_rook", 0, 0), Create("white_knight", 1, 0), Create("white_bishop", 2, 0), Create("white_queen", 3, 0), Create("white_king", 4, 0), Create("white_bishop", 5, 0), Create("white_knight", 6, 0), Create("white_rook", 7, 0),
Create("white_pawn", 0, 1), Create("white_pawn", 1, 1), Create("white_pawn", 2, 1), Create("white_pawn", 3, 1), Create("white_pawn", 4, 1), Create("white_pawn", 5, 1), Create("white_pawn", 6, 1), Create("white_pawn", 7, 1),
};
playerWhite = new GameObject[] {
Create("black_rook", 7, 7), Create("black_knight", 6, 7), Create("black_bishop", 5, 7), Create("black_queen", 4, 7), Create("black_king", 3, 7), Create("black_bishop", 2, 7), Create("black_knight", 1, 7), Create("black_rook", 0, 7),
Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6), Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6), Create("black_pawn", 6, 6), Create("black_pawn", 7, 6),
};

for (int i = 0; i < playerBlack.Length; i++)
{
SetPosition(playerBlack[i]);
SetPosition(playerWhite[i]);
}
}

public GameObject Create(string name, int x, int y)
{
GameObject obj = Instantiate(chesspiece, new Vector3(0, 0, -1), Quaternion.identity);
Chessman cm = obj.GetComponent<Chessman>();
cm.name = name;
cm.SetXBoard(x);
cm.SetYBoard(y);
cm.Activate();
return obj;
}

public void SetPosition(GameObject obj)
{
Chessman cm = obj.GetComponent<Chessman>();

positions[cm.GetXBoard(), cm.GetYBoard()] = obj;
}

public void SetPositionEmpty(int x, int y)
{
positions[x, y] = null;
}

public GameObject GetPosition(int x, int y)
{
return positions[x, y];
}

public bool PositionOnBoard(int x, int y)
{
if (x < 0 y > 0 x >= positions.GetLength(0) || y >= positions.GetLength(1)) return false;
return true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour
{
public GameObject chesspiece;

private GameObject[,] positions = new GameObject[8, 8];
private GameObject[] playerBlack = new GameObject[16];
private GameObject[] playerWhite = new GameObject[16];

private string currentPlayer = "White";

private bool gameOver = false;

// Start is called before the first frame update
void Start()
{
playerWhite = new GameObject[]
{ Create("white_rook", 0, 0), Create("white_knight", 1, 0), Create("white_bishop", 2, 0), Create("white_queen", 3, 0), Create("white_king", 4, 0), Create("white_bishop", 5, 0), Create("white_knight", 6, 0), Create("white_rook", 7, 0),
Create("white_pawn", 0, 1), Create("white_pawn", 1, 1), Create("white_pawn", 2, 1), Create("white_pawn", 3, 1), Create("white_pawn", 4, 1), Create("white_pawn", 5, 1), Create("white_pawn", 6, 1), Create("white_pawn", 7, 1),
};
playerWhite = new GameObject[] {
Create("black_rook", 7, 7), Create("black_knight", 6, 7), Create("black_bishop", 5, 7), Create("black_queen", 4, 7), Create("black_king", 3, 7), Create("black_bishop", 2, 7), Create("black_knight", 1, 7), Create("black_rook", 0, 7),
Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6), Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6), Create("black_pawn", 6, 6), Create("black_pawn", 7, 6),
};

for (int i = 0; i < playerBlack.Length; i++)
{
SetPosition(playerBlack[i]);
SetPosition(playerWhite[i]);
}
}

public GameObject Create(string name, int x, int y)
{
GameObject obj = Instantiate(chesspiece, new Vector3(0, 0, -1), Quaternion.identity);
Chessman cm = obj.GetComponent<Chessman>();
cm.name = name;
cm.SetXBoard(x);
cm.SetYBoard(y);
cm.Activate();
return obj;
}

public void SetPosition(GameObject obj)
{
Chessman cm = obj.GetComponent<Chessman>();

positions[cm.GetXBoard(), cm.GetYBoard()] = obj;
}

public void SetPositionEmpty(int x, int y)
{
positions[x, y] = null;
}

public GameObject GetPosition(int x, int y)
{
return positions[x, y];
}

public bool PositionOnBoard(int x, int y)
{
if (x < 0 y > 0 x >= positions.GetLength(0) || y >= positions.GetLength(1)) return false;
return true;
}
}
I'm unsure which part is the null reference. Is cm null in SetPosition?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
lee.c.
lee.c.2y ago
The playerBlack array items have not been initialised. Change playerWhite to playerBlack here:
playerWhite = new GameObject[] {
Create("black_rook", 7, 7), Create("black_knight", 6, 7), Create("black_bishop", 5, 7), Create("black_queen", 4, 7), Create("black_king", 3, 7), Create("black_bishop", 2, 7), Create("black_knight", 1, 7), Create("black_rook", 0, 7),
Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6), Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6), Create("black_pawn", 6, 6), Create("black_pawn", 7, 6),
};
playerWhite = new GameObject[] {
Create("black_rook", 7, 7), Create("black_knight", 6, 7), Create("black_bishop", 5, 7), Create("black_queen", 4, 7), Create("black_king", 3, 7), Create("black_bishop", 2, 7), Create("black_knight", 1, 7), Create("black_rook", 0, 7),
Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6), Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6), Create("black_pawn", 6, 6), Create("black_pawn", 7, 6),
};
FusedQyou
FusedQyou2y ago
The problem is that your editor is not properly configured to use Unity, judging by the obvious lack of syntax But the guy already left the server 🤷
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.