Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by GadgetGirls · Sep 19, 2016 at 03:47 PM · c#nullsingletonunassiunassignedreferenceexception

if (gameobject != null) giving UnassignedReferenceException

So I'm creating a game in which the player has several playing pieces to choose from. I've created a game manager which is a singleton (and I think that might be part of my problem). The pieces are Unity UI buttons, and when they are clicked a script on the Manger is called which stores which piece was clicked on. If another piece was already selected, I want to forget about the piece and as a visual cue to the player I'm adjusting the scale. So here's my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Manager : MonoBehaviour {
 
     public static Manager manager;
     GameObject chosenPiece = null;
 
     void Awake()
     {
         if (manager == null)
         {
             //DontDestroyOnLoad(gameObject);
             manager = this;
         } else if (this != manager)
         {
             Destroy(gameObject);
         }
     }
 
 public void PieceClicked(GameObject thisPiece)
     {
         Vector3 scaleFactor = new Vector3(0.1F, 0, 0.1F);
         if (chosenPiece != null)
         {
             chosenPiece.transform.localScale -= scaleFactor;
         }
 
         thisPiece = chosenPiece;
         chosenPiece.transform.localScale += scaleFactor;
     }
 

Trouble is when I run it, I get this error

UnassignedReferenceException: The variable chosenRune of Manager has not been assigned. You probably need to assign the chosenRune variable of the Manager script in the inspector.

I also tried changing around my if statement so that it was if (chosePiece == null) and even tried if(chosenPiece == null || chosenPiece.Equals(null)) but I got the same errors that way.

Most people who have similar error seem to have an issue with the script being attached to multiple objects. I've gone through and double checked, and there is only one version of the Manager Script in the scene once I've hit play (before as well). So why can't I check if my chosenPiece is null?

Comment
Add comment · Show 2
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 Bonfire-Boy · Sep 19, 2016 at 04:04 PM 0
Share

Are you sure it's not line 31 that's throwing the exception? You access chosenPiece there without checking if it's null (and presumably it can be null, since you're checking for that just beforehand).

It's far from clear what that localScale-related code's supposed to do, by the way. It just subtracts something then adds it back on again, so it has no effect.

avatar image GadgetGirls Bonfire-Boy · Sep 19, 2016 at 04:52 PM 0
Share

So yeah, it was line 31, I misspoke before, in that because I had line 30 backwards line 31 was throwing the exception.

Just so you know, the point of localScale is to show the user what the currently selected piece is. The idea is that if no piece is selected we assign the one the user clicked on to chosenPiece and then make the UI button slightly bigger so there is a visual cue as to the Player to re$$anonymous$$d them which piece they chose.

But if a piece was already chosen before, I want it to go back to the normal size so that the pieces don't continue to grow and grow and grow each time a piece is selected. That why I both add and subtract from local scale.

2 Replies

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

Answer by szimmermann · Sep 19, 2016 at 04:01 PM

I think the error lies in line 30

   thisPiece = chosenPiece;

You never assign a value to chosenPiece. In the next line you write

 chosenPiece.transform.localScale += scaleFactor;

which gives you an UnassignedReferenceException because chosenPiece still has not been assigned. If you change line 30 to

 chosenPiece=thisPiece;

you should get rid of that exception.

Comment
Add comment · Show 1 · 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 GadgetGirls · Sep 19, 2016 at 04:07 PM 0
Share

Okay thank you. You were right, I did have the line backwards. I just assumed the code was never getting that far so I didn't even check because for some reason I thought the error was on line 25. Which probably means I had another issue I fixed, and then didn't realize the error 'moved'.

Thanks

avatar image
0

Answer by Bunny83 · Sep 19, 2016 at 04:06 PM

Uhm, the error is most likely thrown on line 31, isn't it?

We really have too much null-reference-exception questions here on UA. If you post a question about such an error post the whole complete error. You can copy and paste it from the console.

btw: this line makes no sense:

 thisPiece = chosenPiece;

"thisPiece" is a parameter of your method. Why would you set it to the chosenPiece? Don't you want to do it the other way round?

 chosenPiece = thisPiece;
Comment
Add comment · Show 1 · 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 GadgetGirls · Sep 19, 2016 at 05:06 PM 0
Share

I misspoke, line 31 was throwing the exception because of the error on line 30. So Line 30 had the error in the code that was causing the issues, just Unity was catching it on line 31. You're were completely right about doing it the other way around.

I do have a question for you, just so in future I can post better questions. You said I should post the full error message. I recreated the original problem just so I could see what the original message looked like and the most useful and relevant line was

$$anonymous$$anager.RuneClicked (UnityEngine.GameObject thisRune) (at Assets/myScripts/$$anonymous$$anager.cs:79)

Except that's because I have a lot more lines of code in my actual manager script than what I posted because I was pretty sure that none of the other lines of code had to do with that error. Also you'll notice in the code I posted I renamed the variables and functions to something that I thought made more sense in a generic sense rather than trying to explain what a rune was for the purpose of my games.

Like I said, I'm just wondering how to better post questions in the future. I notice most people use rather generic variable names when they post questions, and I don't know if that reflects what's in their actual code or not. Should I have bothered to rename my variables to something more generic?

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

221 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Unassigned Reference when using GetComponent on Instantiated Objects,How do I use Get_Component on instantiated objects 1 Answer

NullReferenceException: Object reference not set to an instance of an object error 0 Answers

UnassignedReferenceException: The variable has not been assigned, even though it has. - c# 3 Answers

NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer

How to get a variable value before it was null ? 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