Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Syntax Error. Having trouble making an incremental counter in Unity for OnMouseDown

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 OnMouseDown()
    {


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


        {
            Debug.Log(i);
        }

    }

}

>Solution :

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}");
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading