1

I'm fairly new to C# so I'm still getting used to it.

I've had this code going where I want to control a character with a virtual joystick. I was testing whether the joystick would be responsive or now, but every time I try to play the scene, I get 3 CS0535 errors saying:

'VirtualJoystick' does not implement interface member UnityEngine.EventSystems.IPointerUpHandler.OnPointerUp(UnityEngine.EventSystems.PointerEventData)'

'VirtualJoystick' does not implement interface member UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)'

'VirtualJoystick' does not implement interface member `UnityEngine.EventSystems.IPointerDownHandler.OnPointerDown(UnityEngine.EventSystems.PointerEventData)'

Here's the code. It's fairly short.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{

private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;

private void start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}

public virtual void onDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
Debug.Log("Test"); 
}
}

public virtual void onPointerDown(PointerEventData ped)
{
onDrag(ped);
}

public virtual void onPointerUp(PointerEventData ped)
{

}

}

I hope you guys could help me through this.

EDIT:

I have corrected the syntax, which was cause of why the scene was not playing. But the Debug.Log on OnDrag code is not working. I'm getting this NullReferenceException: Object reference not set to an instance of an object error. Again, I'm just following what the video says, and his seems to work just fine.

Peter Balanag
  • 11
  • 1
  • 5

4 Answers4

3

You have declared the methods virtual, since the base class declares them virtual already, you have to declare them with override.

So:

public override void OnPointerUp(PointerEventData ped) { }

Also note that with C# your naming should match exact, so OnPointerUp instead of onPointerUp.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • You could also add the new keyword in front of your method definition i think – Jordy van Eijk Jan 29 '16 at 13:02
  • **Never ever do that** @JordyvanEijk It will not get called when the method is called on the base class. `new` breaks inheritance. – Patrick Hofman Jan 29 '16 at 13:02
  • I know that but that is also a possibility. depending on what you want to achieve. – Jordy van Eijk Jan 29 '16 at 13:06
  • Suggesting that if OP wants to override a method is a very bad idea. – Patrick Hofman Jan 29 '16 at 13:06
  • Of course, error on the Syntax. Thanks for pointing that out. I got it to play the scene after fixing it, but there was this NullReferenceException error when I was testing it with the Log saying: NullReferenceException: Object reference not set to an instance of an object. What should be my next step here? – Peter Balanag Jan 29 '16 at 13:13
1

Correct your spelling, i.e. onPointerDown -> OnPointerDown. C# is case sensitive.

romanoza
  • 4,775
  • 3
  • 27
  • 44
0

When your class inherit from interface(s), it should implement all methods and properties declared on such interface(s).

In your case you must declare in your class the methods mentioned in the compiling error messages, respecting their parameters type and returning values, all case sensitive.

Interfaces are meant to make sure all classes that inherit from them implement such methods and properties. Therefore you could access those members without more knowledge about the class itself.

Interfaces do not declare method/property modifiers (private, public, etc). You can specify them as you see fit.

mycelo
  • 409
  • 5
  • 4
-2

idraghandler,ipointeruphandler,ipointerdownhandler implement in this order

bha
  • 1