Home > Software engineering >  C# Syntax Error. Having trouble making an incremental counter in Unity for OnMouseDown
C# Syntax Error. Having trouble making an incremental counter in Unity for OnMouseDown

Time:10-01

Basically, having a lot of issues trying to implement some code I'm working on in Unity. I want to have it so that when I click on the object, it will write to the Debug console a number, that increases by 5 each time it is clicked up to a max of 50.

But for some reason my script doesn't want to attach, I've got a syntax error that I can't quite figure out, does anyone have any ideas? Would really appreciate any help.

Here's my current code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Supercyl : MonoBehaviour
{
    private int i = 5;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void onm ouseDown()
    {


        if i >= 50;
             Debug.log(i);
             i = i   5;


        {
            Debug.Log(i);
        }

    }

}

CodePudding user response:

The syntax error you are getting is most likely related to completely misformatted if statement. The correct syntax is

if (condition)
   {
     // code here
   }

Secondly, if you want to react to click on an object use IPointerClickHandler interface, from UnityEngine.EventSystems namespace. Remember to add PhysicsRaycaster component to your camera if the object is not UI. The clicked object also needs to have a collider

using UnityEngine;
using UnityEngine.EventSystems;

public class Supercyl : MonoBehaviour, IPointerClickHandler
{
    private int counter = 0;
    public void OnPointerClick(PointerEventData eventData)
    {
        counter  = 5;
        Debug.Log($"Counter value {counter}");
    }
}
  • Related