- Home /
GameObject not being recognized
I'm following a simple tutorial on making a cookie clicker. I am literally on the first step in scripting but the public GameObject is not being recognized and I can't add an object to it in the inspector.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Policy;
using UnityEngine;
public class ButtonBehavior : MonoBehaviour
{
public GameObject textBox;
public void whenButtonPressed()
{
textBox.SetActive(true);
}
}
I've fixed your code formatting and deleted your other questions. You posted the same question 3 times.
Answer by rainChu · Oct 21, 2020 at 06:55 PM
A few things. First of all, are you sure that System.Security.Policy is even available to you? It's best to delete that line, as you're not using it. In my project, at least, it's not even an available namespace and will prevent you from adding this script to a GameObject, because it's not compiling successfully.
Check the error window for further errors. Errors contain useful information about what is wrong. Usually, if you learn the terminology, they will spell out quite simply what needs to be fixed about your code.
Also, make sure you're formatting your code correctly so it's easier to read. Here's how I would format it:
using System; // For now, it's safe to delete these three line but you might need them again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonBehavior : MonoBehaviour
{
public GameObject textBox;
public void whenButtonPressed( )
{
textBox.SetActive( true );
}
}
When posting to Unity Answers, be sure to use the code formatting button, which automatically indents it to use the code markup.
And one last thing- For MonoBehaviour scripts, be sure that the name of the file is the same as the class name, or Unity won't be able to add the script. Because your class is called ButtonBehavior
, be sure that your file is named ButtonBehavior.cs
This is also good practice in general, and you should stick to it.
If you've checked these things, and you still have problems, let me know and I'll try to help.