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
-1
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
Add comment · Show 3
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 SkaredCreations · Dec 24, 2014 at 12:14 PM 2
Share

You missed to specify the exact exception text, since it contains the line number where the error is being raised.

avatar image SpaceFiveFive · Dec 25, 2014 at 03:57 PM 0
Share

Fixed it.

avatar image SpaceFiveFive · Dec 28, 2014 at 09:27 PM 0
Share

Any ideas, anyone?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Reference variable in other script 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Null Reference at (wrapper managed-to-native) 0 Answers

Despawner script returns null reference exception. 0 Answers

Why is this code getting NullReferenceException 2 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