Search code examples
c#unity-game-engineunity3d-2dtoolscursor-position

Using PointerEventData returns error: "Unexpected symbol"


I am new to C# and decided to start using it for now since most of the Unity tutorials are focused on it and I've never used JavaScript for anything other than webpages.

For the most part everything was working until I tried using the Vector2.MoveTowards method:

    void OnMouseDown() {
    int weapCoolDown = 1;
    Instantiate(bullet_player);
    Vector2.MoveTowards(GameObject.Find("player-1"), Vector2 PointerEventData.position(), float p1BulletSpeed);
}

Errors reported by Unity console

I tried removing Vector2, then it asks me to add EventSystems but it's already there inside UnityEngine. I fix that and then it says it doesnt exist? Then I fix that and it just gives me a bunch of other errors that I dont even really know how to remove.

And when I try using an identifier instead, it will assign it but it won't let me use it and it says Unexpected Symbol like usual.

The Unity manual doesn't provide enough detail or examples to really help me solve this, is there a class or reference I am missing or should I just try and use an alternate method?


Solution

  • There are a few things you should review in your code:

    The Vector2.MoveTowards() is not being called with the proper parameters. GameObject.Find() will return a gameObject, not a Vector2. If you're looking for the current position of player 1, you should construct a Vector2 from player 1's transform.position.

    Vector2 playerPosition = new Vector2();
    playerPosition.x = GameObject.Find("player-1").transform.position.x;
    playerPosition.y = GameObject.Find("player-1").transform.position.y;
    

    Any use of PointerEventData requires a using directive for EventSystem. In order to include it, add using UnityEngine.EventSystem with the rest of your using directives.

    The Vector2 prefix before PointerEventData.position() is invalid. It's not an explicit typecast, as it's not in parenthesis, but typecasts aren't needed since position will return a Vector2.

    pointerEventData.position is a property that draws from an object reference. However, the implementation of PointerEventData.position() is improperly using this as a static method. Since it's not actually a static method, but a dynamic property, it will fail to compile and draw errors.

    For pointerEventData to exist, it needs to originate from an event which retrieves eventData. Unfortunately, OnMouseDown() does not do this. However, an interface method from IPointerClickHandler called OnPointerClick(PointerEventData pointerEventData) has the pointerEventData you need.

    Refactoring your existing code, this is closer to the functionality you're looking for.

    // Add this "using" directive
    using UnityEngine.EventSystems;
    
    // Be sure to implement the interface of IPointerClickHandler
    public class NameOfYourScript : MonoBehaviour, IPointerClickHandler {
    
        //
        // Additional Code in your class. . . .
        // 
    
        // Replace OnMouseDown() with this.
        public void OnPointerClick(PointerEventData pointerEventData) {
            Instantiate(bullet_player);
    
            Vector2 playerPosition = new Vector2();
            playerPosition.x = GameObject.Find("player-1").transform.position.x;
            playerPosition.y = GameObject.Find("player-1").transform.position.y;
    
            Vector2.MoveTowards(playerPosition, pointerEventData.position, p1BulletSpeed);
        }
    }