lo looolo

lo looolo

  • NA
  • 7
  • 731

Launch a method from another class

Feb 20 2018 5:57 AM
Hello, i've got a problem in my code, i use Unity and i have 2 C# classes , I want to call the method "OnButtonPressed(VirtualButtonBehaviour vb)" from the second one called ObjectHighlighter.cs , sorry i'am pretty new on C# but i need to find a solution , i'am doing something wrong on the second class i've got an error once i call the method from the second class wich is:
 
"NullReferenceException: Object reference not set to an instance of an object
ObjectHighlighter.Update () (at Assets/Scripts/ObjectHighlighter.cs:45)"
 
Any ideas ? Thank you !
 
- My First class:
 
public class VB_Anim : MonoBehaviour, IVirtualButtonEventHandler {
public static GameObject vbBtnObj;
public Animator immoAni;
// Use this for initialization
void Start () {
vbBtnObj = GameObject.Find("Vb_Rotate");
vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
immoAni.GetComponent<Animator>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb)
{
immoAni.enabled = true;
immoAni.Play("immo_rotate_animation");
Debug.Log("BTN Pressed the Animation is running");
}
public void OnButtonReleased(VirtualButtonBehaviour vb)
{
immoAni.enabled = false;
}
// Update is called once per frame
void Update () {
}
}
-Here is my second class called ObjectHighlighter.cs
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHighlighter : MonoBehaviour
{
public Animator anim;
public static VirtualButtonBehaviour vbBtnObj;
public Animator immoAni;
public VB_Anim vbscript;
// initialization
void Start()
{
}
void Update () {
foreach (Touch touch in Input.touches)
{
Debug.Log("ok...");
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, 100000))
{
vbscript.OnButtonPressed(vbBtnObj);
}
}
}
}
}

Answers (3)