0
\$\begingroup\$

I am writing the code for my button, but it's giving me errors like this:

Assets\Scripts\SteamIntegration.cs (60, 6): 'Button' is not an attribute class

Where did I make a mistake in this code?

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

public class SteamIntegration : MonoBehaviour
{
    public Button button;

    void Start()
    {
        button.onClick.AddListener(() => {
        });        

        try
        {
            Steamworks.SteamClient.Init(/*AppID here*/);
        }
        catch (System.Exeption e)
        {
            Debug.Log(e);
        }
    }
   
    // ...
    [Button]
    public void IsThisAchievementUnlocked(string id)
    {
        var ach = new Steamworks.Data.Achievement(id);
        Debug.Log($"Achievement {id} status: " + ach.State);
    }


    [Button]
    public void UnlockAchievement(string id)
    {
        var ach = new Steamworks.Data.Achievement(id);
        ach.Trigger();

        Debug.Log($"Achievement {id} unlocked");
    }
}


    [Button]
    public void ClearAchievementStatus(string id)
    {
        var ach = new Steamworks.Data.Achievement(id);
        ach.Clear();

        Debug.Log($"Achievement {id} cleared");
    }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please share code and error messages as text, not as images. Can you also explain why you're adding [Button] attributes above your achievement methods? \$\endgroup\$
    – DMGregory
    Sep 28 at 15:29

1 Answer 1

2
\$\begingroup\$

When you put a word in square brackets before a type, variable, or method declaration, that's called an Attribute.

Attributes are special classes that annotate the declaration with metadata and can be used via reflection as hints for code generation, serialization, or drawing appropriate controls in Inspector UI. For example, when you write [Range(0, 1)] over a public or serialized float variable, that signals to the Unity inspector to draw that field as a slider instead of just a numeric input box. The RangeAttribute class is the one that stores this metadata about the field.

When you write [Button] above a method, the compiler is going to look for a class named Button or ButtonAttribute that can be used as an attribute to annotate that method. It searches the namespaces you've imported with your using statements at the top of the file.

In this case, the only Button it can find is UnityEngine.UI.Button, and that's descended from UnityEngine.Component, not System.Attribute. So the compiler is telling you that you cannot use this class as though it were an attribute, when it isn't one.

Maybe you meant to refer to a different thing also named Button? If so, it's likely that it's in a different namespace, and you need to import that namespace with using Somenamespace; or specify the exact path to it, like [SomeNamespace.Button] instead of the unqualified [Button] each time you use it.

If you go the using route, you may need to disambiguate, something like...

using UIButton = UnityEngine.UI.Button;
using InspectorButton = Somenamespace.ButtonAttribute;

...ensuring you can unambiguously refer to the two similarly-named types with distinct names of your choosing.

Or these attributes might have been added in error. Maybe you copied them from an example where someone was using a library similar to this one to add clickable buttons in the Inspector for the component, but that library is not a part of your project at the moment. If you want this functionality, you'll need to include the library and update your references to its namespace/attributes as above.

Or, if you don't want this functionality, you can simply delete the [Button] lines to clear this error.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .