Unexpected symbol `object' in class, struct, or interface member declaration.
Unity reported this error about my program after I had finished making a C# script. I need this error to be fixed soon. Here's the script exactly:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchDisplay : MonoBehaviour {
int object;
// Use this for initialization
void Start () {
int object = GameObject.FindWithTag("LoginBtn").SetActive = "true";
int object = GameObject.FindWithTag("UserField").SetActive = "true";
int object = GameObject.FindWithTag("PassField").SetActive = "true";
int object = GameObject.FindWithTag("SaveLogin").SetActive = "true";
int object = GameObject.FindWithTag("CreateNew").SetActive = "true";
int object = GameObject.FindWithTag("AlphaField").SetActive = "false";
int object = GameObject.FindWithTag("CancelBtn").SetActive = "false";
}
// Update is called once per frame
void Update () {
}
void TaskOnClick () {
if (gameObject.tag == "CreateNew") {
int object = GameObject.FindWithTag("LoginBtn").SetActive = "true";
int object = GameObject.FindWithTag("UserField").SetActive = "true";
int object = GameObject.FindWithTag("PassField").SetActive = "true";
int object = GameObject.FindWithTag("SaveLogin").SetActive = "false";
int object = GameObject.FindWithTag("CreateNew").SetActive = "false";
int object = GameObject.FindWithTag("AlphaField").SetActive = "true";
int object = GameObject.FindWithTag("CancelBtn").SetActive = "true";
}
if (gameObject.tag == "CancelBtn") {
int object = GameObject.FindWithTag("LoginBtn").SetActive = "true";
int object = GameObject.FindWithTag("UserField").SetActive = "true";
int object = GameObject.FindWithTag("PassField").SetActive = "true";
int object = GameObject.FindWithTag("SaveLogin").SetActive = "true";
int object = GameObject.FindWithTag("CreateNew").SetActive = "true";
int object = GameObject.FindWithTag("AlphaField").SetActive = "false";
int object = GameObject.FindWithTag("CancelBtn").SetActive = "false";
}
}
}
Answer by Bunny83 · Mar 25, 2018 at 02:13 AM
You have countless issues in your script.
First of all you try to use "object" as variable name. However object is a hardcoded alias for the type System.Object (just like "string" is an alias for System.String and "float" is an alias for System.Single).
The class member variable you declared isn't used at all since you declare a local variable inside your methods
You try to declare a variable with the same name more than once within a single scope (method). Variable names need to be unique.
SetActive is a method but you use it like a variable or property. You can not assign a value to a method. You have to call the method and pass a boolean value (true or false).
SetActive doesn't return any value so when invoking SetActive you can not assign the result to an int variable.
Currently you try to "assign" the string "true" or "false" to SetActive. However a string is not a boolean value you have to use
true
orfalse
, not"true"
and"false"
Apart from all the other issues it's poinless to declare a local variable, assign a value to it when you don't do anything with that variable.
Those were just your plain syntax errors. Now there are a few conceptional errors:
FindWithTag can only find active gameobject. That means when you deactivate an object you can no longer find it with that method. For performance reasons it would be a good idea to store the references in variables anyways. It's actually simpler to just make a few public variables and assign the objects in the inspector
Remove empty Unity callbacks like Update as they are bad for performance and clutter the class.
So you may want a script like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchDisplay : MonoBehaviour
{
public GameObject LoginBtn;
public GameObject UserField;
public GameObject PassField;
public GameObject SaveLogin;
public GameObject CreateNew;
public GameObject AlphaField;
public GameObject CancelBtn;
void Start ()
{
LoginBtn = GameObject.FindWithTag("LoginBtn");
UserField = GameObject.FindWithTag("UserField");
PassField = GameObject.FindWithTag("PassField");
SaveLogin = GameObject.FindWithTag("SaveLogin");
CreateNew = GameObject.FindWithTag("CreateNew");
AlphaField = GameObject.FindWithTag("AlphaField");
CancelBtn = GameObject.FindWithTag("CancelBtn");
AlphaField.SetActive(false);
CancelBtn.SetActive(false);
}
void TaskOnClick ()
{
if (gameObject.tag == "CreateNew")
{
LoginBtn.SetActive(true);
UserField.SetActive(true);
PassField.SetActive(true);
SaveLogin.SetActive(false);
CreateNew.SetActive(false);
AlphaField.SetActive(true);
CancelBtn.SetActive(true);
}
else if (gameObject.tag == "CancelBtn")
{
LoginBtn.SetActive(true);
UserField.SetActive(true);
PassField.SetActive(true);
SaveLogin.SetActive(true);
CreateNew.SetActive(true);
AlphaField.SetActive(false);
CancelBtn.SetActive(false);
}
}
}
Note that you never actually deactivate the LoginBtn, UserField and PassField object so i'm not sure if it's actually necessary to set them active. Keep in mind that FindWithTag can only find active objects. If you want them to be deactivated from the start you should remove all the FindWithTag calls and just assign the objects in the inspector.
Thanks for the script, Unity reports no errors about it. But there is just one problem: When I click on the script in Project/Assets, on the top of the Inspect tab it shows all of the public gameobjects created, but their blank. When I click on them to set them to the objects, it doesn't show anything to select.
Your answer
Follow this Question
Related Questions
C# unexpected symbol error 1 Answer
Unity Crashes When Clicking Play: Script Error? 1 Answer
ForcedScopedThreadAttach 0 Answers
Destroy a Prefab from an Array? (C#) 2 Answers
I have an error on a C# script @username 2 Answers