- Home /
UnassignedReferenceException
I am a complete newbie when it comes to scripting/programming. I am following along to a tutorial to make a matching tile game.
This is the error that I keep getting
UnassignedReferenceException: The variable matchTwo of 'TileGenerator' has not been assigned. You probably need to assign the matchTwo variable of the TileGenerator script in the inspector. TileGenerator.revealCardTwo () (at Assets/Assets/Scripts/TileGenerator.js:80) TileGenerator.Update () (at Assets/Assets/Scripts/TileGenerator.js:50)
This is the code
var hit : RaycastHit;
var numberOfTiles = 16;
var tileLocations = new Array ( //Array to hold the tiles
Vector3 (0,0,0), Vector3 (1.5,0,0),
Vector3 (3,0,0), Vector3 (4.5,0,0),
Vector3 (0,1.5,0), Vector3 (1.5,1.5,0),
Vector3 (3,1.5,0), Vector3 (4.5,1.5,0),
Vector3 (0,3,0), Vector3 (1.5,3,0),
Vector3 (3,3,0), Vector3 (4.5,3,0),
Vector3 (0,4.5,0), Vector3 (1.5,4.5,0),
Vector3 (3,4.5,0), Vector3 (4.5,4.5,0));
var matchOne : GameObject;
var tileObjects : GameObject [];
var tileName1 : String;
var tName1 : Array; //creates an array to hold the names of the cards to be called
var matchTwo : GameObject;
var tileName2 : String;
var tName2 : Array;
function Start ()
{
Camera.main.transform.position = Vector3 (2.25,2.25, -8);
for ( var i = 0; i < numberOfTiles; i++) // places tiles in grid increasing them until it reaches 16
{
Instantiate (tileObjects[i], tileLocations[i], Quaternion.identity); //
}
}
function Update ()
{
if (Input.GetButtonDown("Fire1")) //checks if the mouse button has been pressed.
var ray1 = Camera.main.ScreenPointToRay(Input.mousePosition); //used to send ray from camera position
{
//print ("Mouse Clicked");
}
{
if (Physics.Raycast(ray1, hit, Mathf.Infinity)) //checks to see if object was hit with the ray
{
//print ("Help me bob I've hit something");
if (!matchOne)
{
revealCardOne ();
}
else
{
revealCardTwo ();
}
}
}
}
function revealCardOne () //reveal card
{
matchOne = hit.transform.gameObject; //reveals card is it has been clicked
tileName1 = matchOne.transform.parent.name;//checks name of the parent object name to allow comparison
(print(tileName1));
if (matchOne == null)
{
//print ("No object found");
}
else
{
tName1 = tileName1.Split ("_"[0]); //places the clones of the tileobjects into array and splits them into substrings
matchOne.transform.Rotate (Vector3(0,180,0));
}
{
//print ("tName1[0]");
}
}
function revealCardTwo ()
{
matchOne = hit.transform.gameObject; //reveals card is it has been clicked
tileName2 = matchTwo.transform.parent.name;//checks name of the parent object name to allow comparison
(print(tileName2));
if (matchTwo == null)
{
//print ("No object found");
}
else
{
tName2 = tileName2.Split ("_"[0]); //places the clones of the tileobjects into array and splits them into substrings
//print ("tName2[0]");
matchTwo.transform.Rotate (Vector3(0,180,0));
}
}
I am aware it is telling me to assign the matchTwo var in the inspector, I shouldn't have to assign it in the inspector. So I declared the matchOne and matchTwo vars as private. This is the error I get when doing that.
Assets/Assets/Scripts/TileGenerator.js(15,29): UCE0001: ';' expected. Insert a semicolon at the end.
I don't understand as to why I am getting this error as there is a semi colon at the end of the line. Here is the code
#pragma strict
var hit : RaycastHit;
var numberOfTiles = 16;
var tileLocations = new Array ( //Array to hold the tiles
Vector3 (0,0,0), Vector3 (1.5,0,0),
Vector3 (3,0,0), Vector3 (4.5,0,0),
Vector3 (0,1.5,0), Vector3 (1.5,1.5,0),
Vector3 (3,1.5,0), Vector3 (4.5,1.5,0),
Vector3 (0,3,0), Vector3 (1.5,3,0),
Vector3 (3,3,0), Vector3 (4.5,3,0),
Vector3 (0,4.5,0), Vector3 (1.5,4.5,0),
Vector3 (3,4.5,0), Vector3 (4.5,4.5,0));
private var matchOne : GameObject;
var tileObjects = GameObject [];
var tileName1 : String;
var tName1 : Array; //creates an array to hold the names of the cards to be called
private var matchTwo : GameObject;
var tileName2 : String;
var tName2 : Array;
function Start ()
{
Camera.main.transform.position = Vector3 (2.25,2.25, -8);
for ( var i = 0; i < numberOfTiles; i++) // places tiles in grid increasing them until it reaches 16
{
Instantiate (tileObjects[i], tileLocations[i], Quaternion.identity); //
}
}
function Update ()
{
if (Input.GetButtonDown("Fire1")) //checks if the mouse button has been pressed.
var ray1 = Camera.main.ScreenPointToRay(Input.mousePosition); //used to send ray from camera position
{
//print ("Mouse Clicked");
}
{
if (Physics.Raycast(ray1, hit, Mathf.Infinity)) //checks to see if object was hit with the ray
{
//print ("Help me bob I've hit something");
if (!matchOne)
{
revealCardOne ();
}
else
{
revealCardTwo ();
}
}
}
}
function revealCardOne () //reveal card
{
matchOne = hit.transform.gameObject; //reveals card is it has been clicked
tileName1 = matchOne.transform.parent.name;//checks name of the parent object name to allow comparison
(print(tileName1));
if (matchOne == null)
{
//print ("No object found");
}
else
{
tName1 = tileName1.Split ("_"[0]); //places the clones of the tileobjects into array and splits them into substrings
matchOne.transform.Rotate (Vector3(0,180,0));
}
{
//print ("tName1[0]");
}
}
function revealCardTwo ()
{
matchOne = hit.transform.gameObject; //reveals card is it has been clicked
tileName2 = matchTwo.transform.parent.name;//checks name of the parent object name to allow comparison
(print(tileName2));
if (matchTwo == null)
{
//print ("No object found");
}
else
{
tName2 = tileName2.Split ("_"[0]); //places the clones of the tileobjects into array and splits them into substrings
//print ("tName2[0]");
matchTwo.transform.Rotate (Vector3(0,180,0));
}
}
Any help would be greatly appreciated. Thank you.
Answer by robertbu · Dec 19, 2013 at 04:02 PM
You cannot get information from a object referred to by a null reference regardless if it is public or private. Nowhere in your code do you assign a value to 'matchTwo', so unless it is assigned in the Inspector, the following (line 80 in your first script) will generate an error:
tileName2 = matchTwo.transform.parent.name;//checks name of the parent object name to allow comparison.
As for making it private, that will not solve your problem, since nothing assigns a value to matchTwo. But the error has nothing to do with making it private. The error is in this line:
var tileObjects = GameObject [];
...which should be:
var tileObjects : GameObject[];
Ok so I changed it to a : but now it gives me the samething again
UnassignedReferenceException: The variable matchTwo of 'TileGenerator' has not been assigned. You probably need to assign the matchTwo variable of the TileGenerator script in the inspector. TileGenerator.revealCardTwo () (at Assets/Assets/Scripts/TileGenerator.js:82) TileGenerator.Update () (at Assets/Assets/Scripts/TileGenerator.js:52)
How do I assign something to it?
Would that not take away from the randomness of the player picking the second tile at will and make it so that it's always set to one tile?
We don't have your full game, so I'm unclear how matchTwo gets set to any value. All I know is that there is nothing in this script that set the value. You can put access to matchTow inside a null check to get around the error. Something like:
if (matchTwo == null) {
print ("No object found: matchTwo is null");
}
else {
tileName2 = matchTwo.transform.parent.name;//checks name of the parent object name to allow comparison
print(tileName2);
tName2 = tileName2.Split ("_"[0]); //places the clones of the tileobjects into array and splits them into substrings
matchTwo.transform.Rotate (Vector3(0,180,0));
}
But this doesn't solve the underlying problem. Somehow matchTwo has to be a value. Either a user has to pick something and that info needs to get communicated to this script, or you may randomly select a tile and assign it, or...
Your answer
Follow this Question
Related Questions
Unknown Identifier 'i' 2 Answers
Applying Damage to enemy script not working 1 Answer
Script Doesn't work as intended 1 Answer
Empty Transform just attaches itself to an a prefab? 0 Answers