-1
\$\begingroup\$

I'm trying to make a simple button perform an action when pressed. Initially I was using vscode, but as I couldn't solve the problem, I switched to visual studio 2017. For some reason the error persists.

The button has been removed from this link:

https://www.c-sharpcorner.com/article/unity-change-scene-on-button-click-using-c-sharp-scripts-in-unity/

Button code (internet sketch):

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

public class SceneChanger: MonoBehaviour {  
    public void Scene1() {  
        SceneManager.LoadScene("Scene1");  
    }  
    public void Scene2() {  
        SceneManager.LoadScene("Scene2");  
    }  
    public void Scene3() {  
        SceneManager.LoadScene("Scene3");  
    }  
}

Button code (my code):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Windows.Forms;

public class botaoJogar : MonoBehaviour
{
    public Button btn_Jogar;

    void Start()
    {
        Button btn = btn_Jogar.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        Debug.Log("You have clicked the button!");
    }
}

Image of the construction in unity:

enter image description here

Before, I was able to solve the CS0236 error using the answers from this link:

https://stackoverflow.com/questions/2883505/the-type-or-namespace-name-button-could-not-be-found-are-you-missing-a-using

The answer says to add the line using System.Windows.Forms; to the button code, but for some reason, now in visual studio, this error doesn't go away.

Errors:

Assets\botajogar.cs(4,22): error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

Assets\botajogar.cs(8,12): error CS0246: The type or namespace name 'Button' could not be found (are you missing a using directive or an assembly reference?)

Some community links, which I accessed:

https://stackoverflow.com/questions/42000798/how-do-i-add-assembly-references-in-visual-studio-code

https://stackoverflow.com/questions/49805204/adding-reference-to-another-project-from-visual-studio-code

https://stackoverflow.com/questions/2883505/the-type-or-namespace-name-button-could-not-be-found-are-you-missing-a-using

https://stackoverflow.com/questions/47412796/system-windows-forms-assembly-reference-in-vs-code

https://stackoverflow.com/questions/46582847/creating-a-windows-forms-application-in-c-sharp-using-dotnet-new

https://stackoverflow.com/questions/40562192/windows-form-application-on-visual-studio-code

https://stackoverflow.com/questions/48496542/forms-does-not-exist-in-the-namespace-system-windows

Other links:

https://docs.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager?view=vs-2019

https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/compiler-messages/cs0246

I don't know what to do anymore, I feel like I tried to do everything I could, but windows forms simply don't work. Visual studio 2017 has no bugs, just unity.

Installation I did in visual studio 2017:

enter image description here

enter image description here

\$\endgroup\$
0
0
\$\begingroup\$

Nothing in the script you've shown references "Button", so you've either shown us the wrong code, or you've linked a misleading Q&A page/quoted a misleading error unrelated to the root problem.

You do not want to use a Windows Forms button in Unity, because you are not building a Windows Forms application. You're building a Unity game.

You more likely want to use a Unity UI button. That's found in the UnityEngine.UI namespace, as a quick search of the Unity documentation will tell you.

Try using UnityEngine.UI; if you want to reference UI components like buttons.

\$\endgroup\$
3
  • \$\begingroup\$ I edited the question, added my code and showed an image of the construction in Unity. I will try to do what you said. \$\endgroup\$ Sep 18 at 23:54
  • \$\begingroup\$ Thanks. I made a terrible mistake, confused the unity button with the windows forms button. I could not solve this "problem" ever. Should I delete this question? Or do you think she might be useful to someone? \$\endgroup\$ Sep 19 at 0:13
  • 1
    \$\begingroup\$ I don't think it's harmful to leave this question here. \$\endgroup\$
    – DMGregory
    Sep 19 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.