So this a code that work woth button.
public class TouchEvent : MonoBehaviour
{
public int currentNmber = 0;
public void TouchDice1()
{
currentNmber = 1;
Debug.Log(currentNmber);
}
}
And this is code that cousl take value from script above.
On the last line of Update function you can see GetComponent<> that i used
public class BezierTest : MonoBehaviour
{
public TouchEvent value;
bool movement = true;
private void Update()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider != null)
{
movement = false;
Debug.Log(GetComponent<TouchEvent>().currentNmber);
}
}
}
Here is a problem that i dont undestand how to solve. What should i put in it? Also there is a Hierarchy for your facilities.
.

It depends. If both scripts are on the same gameobject then you just need to call GetComponent() and then it would look something like this:
public class BezierTest : MonoBehaviour
{
TouchEvent value;
int num;
bool movement = true;
private void Start()
{
value = GetComponent<TouchEvent>();
}
private void Update()
{
num = value.currentNmber;
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider != null)
{
movement = false;
Debug.Log(GetComponent<TouchEvent>().currentNmber);
}
}
}
In this case you don’t need to do anything in the inspector. Otherwise, if the two scripts are located on different gameobjects you will have to first create a public GameObject in your script and get the component from there. The code should then look like this:
public class BezierTest : MonoBehaviour
{
public GameObject target;
TouchEvent value;
int num;
bool movement = true;
private void Start()
{
value = target.GetComponent<TouchEvent>();
}
private void Update()
{
num = value.currentNmber;
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider != null)
{
movement = false;
Debug.Log(GetComponent<TouchEvent>().currentNmber);
}
}
}
In the inspector all you need to do then is drag the wanted gameobject from the hierarchy to the public value ‘target’
I just tryed and it doesnt work. Now there is no eceptions but currentNumber is always 0 whatever i press. Any ideas how to fix this?