- Home /
Question by
SpaceFiveFive · Dec 24, 2014 at 12:12 PM ·
javascripterrorgameobjectreferencenull
[UNSOLVED] Object reference not set to an instance of an object
Hello!
I've got the code below run in tandem with some VERY specific naming schemes, and I'm trying to find the GameObject directly next to it (if it's a horizontal piece, then above and below, vertical gets to the right and left), and it's giving the error shown in the name when the object is clicked on. If you need any more info on the game itself, please don't be afraid to ask.
Code:
// Made for "Square"
// Game and code made by SpaceGaming Studios
//
//
// (c) 2014 SpaceGaming Studios
// Programming by Eamonn "Space" Nugent
var redLine : GameObject;
var blueLine : GameObject;
var x : int;
var y : int;
var usable : boolean;
var cube : GameObject;
static var isHit : boolean;
var rot : double;
static var isRed : boolean;
static var isBlue : boolean;
static var turnChanged : double;
static var linename;
static var col : boolean;
static var designation : int;
static var crd : int;
static var spotd : int;
static var crd1 : String;
static var spotd1 : String;
static var crd2 : String;
static var spotd2 : String;
var crUp : GameObject;
var crDown : GameObject;
var spUp : GameObject;
var spDown : GameObject;
var crUpStr : String;
var crDownStr : String;
var spUpStr : String;
var spDownStr : String;
var upUsed : boolean;
var downUsed : boolean;
var rightUsed : boolean;
var leftUsed : boolean;
var lTenS : boolean;
var lTenCR : boolean;
var lTenSU : boolean;
var lTenCRU : boolean;
var lTenSD : boolean;
var lTenCRD : boolean;
function Start()
{
x = transform.position.x;
y = transform.position.y;
if (transform.rotation.z > 0)
{
rot = 90;
}
else
{
rot = 0;
}
usable = true;
linename = gameObject.name;
if (linename.Substring(0, 1) == "C")
{
designation = 0;
col = true;
//Debug.Log("DESIGNATION " + designation);
}
if (linename.Substring(0, 1) == "R")
{
designation = 1;
col = false;
//Debug.Log("DESIGNATION " + designation);
}
crd2 = linename.Substring(2, 1);
crd1 = linename.Substring(1, 1);
crd = parseInt(crd1)*10 + parseInt(crd2);
if (crd < 10)
{
lTenCR = true;
}
if (crd < 9)
{
lTenCRU = true;
}
if (crd < 10)
{
lTenCRD = true;
}
//Debug.Log("CRD1 " + crd1);
//Debug.Log("CRD2 " + crd2);
//Debug.Log("CRD " + crd);
spotd1 = linename.Substring(5, 1);
spotd2 = linename.Substring(6, 1);
spotd = parseInt(spotd1)*10 + parseInt(spotd2);
if (spotd < 10)
{
lTenS = true;
}
if (spotd < 9)
{
lTenSU = true;
}
if (spotd < 10)
{
lTenSD = true;
}
//Debug.Log("SPOTD1 " + spotd1);
//Debug.Log("SPOTD2 " + spotd2);
//Debug.Log("SPOTD " + spotd);
define();
}
function Update()
{
if (!VarDefiner.isMainMenu)
{
if (Input.GetMouseButton(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
if (hit.collider.gameObject.name==gameObject.name)
{
isHit = true;
}
else
{
isHit = false;
}
}
}
lineButton();
}
}
function lineButton()
{
if(!VarDefiner.isMainMenu)
{
if(isHit && usable)
{
if(VarDefiner.redTurn)
{
cube = Instantiate(redLine, new Vector3(transform.position.x, transform.position.y, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, rot));
usable = false;
isHit = false;
isRed = true;
VarDefiner.turnNum++;
turnChanged = VarDefiner.turnNum;
VarDefiner.redTurn = false;
check();
}
else
{
cube = Instantiate(blueLine, new Vector3(transform.position.x, transform.position.y, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, rot));
usable = false;
isHit = false;
isBlue = true;
VarDefiner.turnNum++;
turnChanged = VarDefiner.turnNum;
VarDefiner.redTurn = true;
check();
}
}
}
}
function define()
{
if (designation == 1 && (crd != 1))
{
if (!col)
{
if (lTenCRD)
{
if (lTenS)
{
crUpStr = "R0" + (crd-1) + "-S0" + spotd;
}
else
{
crUpStr = "R0" + (crd-1) + "-S" + spotd;
}
}
else
{
if (lTenS)
{
crUpStr = "R" + (crd-1) + "-S0" + spotd;
}
else
{
crUpStr = "R" + (crd-1) + "-S" + spotd;
}
}
Debug.Log(crDownStr);
crDown = GameObject.Find(crDownStr);
}
if (designation == 1 && (crd != 13))
{
if (lTenCRU)
{
if (lTenS)
{
crUpStr = "R0" + (crd+1) + "-S0" + spotd;
}
else
{
crUpStr = "R0" + (crd+1) + "-S" + spotd;
}
}
else
{
if (lTenS)
{
crUpStr = "R" + (crd+1) + "-S0" + spotd;
}
else
{
crUpStr = "R" + (crd+1) + "-S" + spotd;
}
}
Debug.Log(crUpStr);
crUp = GameObject.Find(crUpStr);
}
}
else
{
if (designation == 0 && (spotd != 12))
{
if (lTenCR)
{
if (lTenSU)
{
crUpStr = "C0" + crd + "-S0" + (spotd+1);
}
else
{
crUpStr = "C0" + crd + "-S" + (spotd+1);
}
}
else
{
if (lTenSU)
{
crUpStr = "C" + crd + "-S0" + (spotd+1);
}
else
{
crUpStr = "C" + crd + "-S" + (spotd+1);
}
}
Debug.Log(spUpStr);
spUp = GameObject.Find(spUpStr);
}
if (designation == 0 && (spotd != 1))
{
if (lTenSD)
{
if (lTenCR)
{
crUpStr = "C0" + crd + "-S0" + (spotd-1);
}
else
{
crUpStr = "C" + crd + "-S0" + (spotd-1);
}
}
else
{
if (lTenCR)
{
crUpStr = "C0" + crd + "-S" + (spotd-1);
}
else
{
crUpStr = "C" + crd + "-S" + (spotd-1);
}
}
Debug.Log(spDownStr);
spDown = GameObject.Find(spDownStr);
}
}
}
function check()
{
if (!col)
{
if(!crUp.usable) //Error occurs here when clicking on the error in Unity
{
upUsed = true;
Debug.Log("Up Used");
}
if(!crDown.usable) //Here too
{
downUsed = true;
Debug.Log("Down Used");
}
}
else
{
if(!spUp.usable) //Here
{
rightUsed = true;
Debug.Log("Right Used");
}
if(!spDown.usable) //And lastly, here
{
leftUsed = true;
Debug.Log("Left Used");
}
}
}
Comment
You missed to specify the exact exception text, since it contains the line number where the error is being raised.