- Home /
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?
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?
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...
Does "cube" have an object assigned?
Are you sure that "_instance" is initialized before "screenScale" function execution?
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!
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
" float xPos = xScreenSize(Screen.width,_instance.camWidth,posX);"
I use that already, camWidth is not the same.
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);
}
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).
Your answer
![](https://koobas.hobune.stream/wayback/20220613085031im_/https://answers.unity.com/themes/thub/images/avi.jpg)