Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jesser1981 · Dec 19, 2013 at 03:37 PM · javascript

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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[];

  
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jesser1981 · Dec 19, 2013 at 05:44 PM 0
Share

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?

avatar image Kiloblargh · Dec 19, 2013 at 06:03 PM 0
Share
 matchTwo = something;
avatar image jesser1981 · Dec 19, 2013 at 06:14 PM 0
Share

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?

avatar image robertbu · Dec 19, 2013 at 07:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Every time I try to open a javascript I get an error saying some confusing things that I will post below. What do they mean, and how do I fix the error? 2 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges