Hello every one im geting null refrence error using the code getcomponent() for my ui image.
I dont know where I am wrong because when I use
debug.log(getcomponent())
Unity detect the component
And im using name space unityengine.ui
I khow how to use the code and I’ve used this code before somewhere else and it worked
It’s very annoying situation
Please help me out.
Update:
Actually I’m trying to make a joystick for android and i was wrong debug.log() doesn’t detect the component it says (null refrence error, an instances of the object…) And here is the code .my entire project depends on this , any possible solutions are welcome
using UnityEngine;
using UnityEngine.UI;
public class joyStick : MonoBehaviour
{
private Vector3 pointA;
private Vector3 pointB;
public Vector3 offset;
public Vector2 bulletDirection;
private Vector2 myTouchPosition;
private Touch myTouch;
private int touchID;
public GameObject outerCircle;
private void Start()
{
outerCircle = GameObject.Find("outerCircle");
}
private void Update()
{
touchSelection();
if (myTouch.phase == TouchPhase.Moved && myTouch.fingerId == touchID)
{
myTouchPosition = myTouch.position;
pointB = new Vector3(myTouchPosition.x, myTouchPosition.y, Camera.main.transform.position.z);
offset = pointB - pointA;
pointB = pointA + Vector3.ClampMagnitude(offset, 60);
offset.Normalize();
transform.position = pointB;
}
if (myTouch.phase == TouchPhase.Ended && myTouch.fingerId == touchID)
{
offset = Vector3.zero;
outerCircle.GetComponent<Image>().enabled = false;
GetComponent<Image>().enabled = false;
}
}
private void touchSelection()
{
int i = 0;
while(i<Input.touchCount)
{
myTouch = Input.GetTouch(i);
myTouchPosition = myTouch.position;
if(myTouch.phase == TouchPhase.Began)
{
if (Input.GetTouch(i).position.x <Screen.width/2)
{
touchID = myTouch.fingerId;
outerCircle.transform.position = myTouchPosition;
outerCircle.GetComponent<Image>().enabled = true;
transform.position = myTouchPosition;
GetComponent<Image>().enabled = true;
}
}
++i;
}
}