Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 alas.eridan · Dec 06, 2012 at 07:01 AM · c#object position

Trying to change position based on screen width. Object reference not set to an instance of an object

I'm trying to modify the position of objects based on the change in screen size. Using the methods I created I keep getting the error "Object reference not set to an instance of an object"

I've set it up to work on one cube...

 using UnityEngine;
 using System.Collections;
 
 public class hwUI : MonoBehaviour {
     static hwUI _instance;
     
     
     public Camera cam; // the camera
     //public GameObject cube;
     
     //the designing size of the ui screen
     public float camWidth; //the screen size the camera displays
     public float camHeight; // the screen height
     
 
     // Use this for initialization
     void Awake () {
         _instance = this;
     }
     
     
     //screen scale stuffs
     public static Vector3 screenScale(float posX, float posY)
     {
         //get the percent change of width and startWidth.
         //Then use that percent with startPos to return the new float.
         float xPos = xScreenSize(Screen.width,_instance.camWidth,posX);
         float yPos = yScreenSize(Screen.height,_instance.camHeight,posY);
         print(xPos);
         
         return new Vector3(xPos,yPos,0);
     }
     
     public static float xScreenSize(float width, float startWidth, float startPosX)
     {
         //width / start -> mult with posX
         float xPos = (width / startWidth) * startPosX;
         return xPos;
     }
     
     public static float yScreenSize(float height, float startHeight, float startPosY)
     {
         float yPos = (height / startHeight) * startPosY;
         return yPos;
     }

my error is on this line: float xPos = xScreenSize(Screen.width,_instance.camWidth,posX);

any ideas?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Dec 06, 2012 at 02:37 PM

You haven't posted the whole story, we can't tell if _instance is instantiated or what cube is, we can guess.

Ensure that your variables in your script exist. What is cube, is it a prefab? is it attached? is _instance a static, what is it's definition, how was it declared? Should Awake really be Start?

Comment
Add comment · Show 16 · 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 alas.eridan · Dec 06, 2012 at 04:47 PM 0
Share

cube is a game object, declared as "public GameObject cube;"

_instance is declared as "static hwUI _instance;" hwUI is this public class that all these methods live inside of. That way I can get access to the public vars such as "camWidth" and use them in a public static method.

I think it has something to do with not using the cube properly in a static method...

avatar image astorga · Dec 06, 2012 at 05:24 PM 0
Share

Does "cube" have an object assigned?

Are you sure that "_instance" is initialized before "screenScale" function execution?

avatar image alas.eridan · Dec 06, 2012 at 05:38 PM 0
Share

ah it is "_instance" that is the problem, replacing it with a stand in float works. Is there something I should be doing between declaring "static hwUI _instance;" and using "_instance.camWidth"?

Thanks for the help, looks like we're getting somewhere!

avatar image astorga · Dec 06, 2012 at 05:48 PM 0
Share

If "camWidth" is the same of screen width, you can use "Screen.width".

Take a look here: http://docs.unity3d.com/Documentation/ScriptReference/Screen.html

avatar image alas.eridan · Dec 06, 2012 at 09:09 PM 0
Share

" float xPos = xScreenSize(Screen.width,_instance.camWidth,posX);"

I use that already, camWidth is not the same.

Show more comments
avatar image
0

Answer by alas.eridan · Dec 07, 2012 at 07:01 PM

astorga here is the bulk of the code:

 using UnityEngine;
 using System.Collections;
 
 public class hwUI : MonoBehaviour {
     static hwUI _instance;
     
     
     public Camera cam; // the camera
     //public GameObject cube;
     
     //the designing size of the ui screen
     //public float camWidth; //the screen size the camera displays
     //public float camHeight; // the screen height
     
 
     
     //screen scale stuffs
     public static Vector3 screenScale(float posX, float posY)
     {
         //get the percent change of width and startWidth.
         //Then use that percent with startPos to return the new float.
         float xPos = xScreenSize(Screen.width,800.0f,posX);
         float yPos = yScreenSize(Screen.height,500.0f,posY);
         print(xPos);
         
         return new Vector3(xPos,yPos,0);
     }
     
     public static float xScreenSize(float width, float startWidth, float startPosX)
     {
         //width / start -> mult with posX
         float xPos = (width / startWidth) * startPosX;
         return xPos;
     }
     
     public static float yScreenSize(float height, float startHeight, float startPosY)
     {
         float yPos = (height / startHeight) * startPosY;
         return yPos;
     }

camWidth and camHeight are substituted with 800.0f and 500.0f. The omitted code from this class is irrelevant.

screenScale is being used from another class that is attached to my UI objects like this:

 void Awake () {
         transform.localPosition = hwUI.screenScale(transform.localPosition.x, transform.localPosition.y);
     
     }
Comment
Add comment · 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
0

Answer by astorga · Dec 07, 2012 at 10:28 PM

Ok, I figured out the situation here.

You are trying to make a singleton class. To make it work the way you want, first you need to make sure _instance is initialized. You can do it in Awake function, like this:

 void Awake(){
     if(!_instance){
         _instance = this;
     }
 }

Additionally, you need to put the "hwUI" script in a GameObject. The name doesn't matter, as well as the location and position. With that, your code will work as expected (even with the inspector values if you want to change).

Also, in your code, there's a missing "}" at the end (probably doesn't happen in your real code).

Comment
Add comment · 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

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Object position to player position in angle and apply to rotating a gui plane? 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