Can't get component in my class

Hello

I have a simple class named tile which has the property:

GameObject Wall {private set; get;}

And the methods:

        public void AddWall(GameObject gameObject)
        {
            GameObject = gameObject;
        }
        public void DestroyWall()
        {
            GameObject.GetComponent<Handler>.DisableAll();
        }

Yet for some reason i can’t get component this way. It says:

error CS0119: Expression denotes a method group', where a variable’, value' or type’ was expected

I am also using UnityEngine, so i don’t understand the problem here.

You say you have a simple class named tile. Is it a Monobehaviour? What is Handler?

Handler is a component (a script) attached to the game object, and has a public method DisableAll.

And Tile class is just a class not connected to mono behaviour. :

public class Tile{ } basically.

I can see three errors off the bat.

The property “Wall” has redundant accessibility modifiers (and both set and get are defined backwards, unsure if thats invalid)
AddWall is simply invalid code, you can’t set a variable to a Type.

  • the code may also be ambiguous (you may be using the wrong gameobject variable).
    and GetComponent is a method group, not a property and needs parenthesis to be used correctly

GetComponent();

This error almost always indicates you forgot to put () after a method call. In this case it’s the GetComponent

Also, it’s probably not a great idea to have a GameObject property named GameObject. Try using something other than that as the property name.

public void AddWall(GameObject myGameObject)
        {
            GameObject = myGameObject;  //how does this even work? 
            this.gameObject = myGameObject; //maybe this is what you're after?  ignore if i'm completely wrong.
        }
        public void DestroyWall()
        {
            this.gameObject.GetComponent<Handler>().DisableAll;
        }

Yeah you have to use () with GetComponent like johne has posted.

If you attach your new script to a game object, within the Update() method you can always get it’s values this way:

this.gameObject

But:

this

Refers to the script.

So you have game objects in your hierarchy and scripts attached to them. To get a script attached to a gameobject you use GetComponent();.To find a gameobject, at least at first, just use GameObject.Find(“objectNameGoesHere”);. You’ll want to eventually switch though to using your game object to find others in the hierarchy like this.gameObject.transform.parent etc etc…

This here:

GameObject

You can never use that in your code, because GameObject is a type of object. You can create a reference to one like this:

GameObject myGameObject;

Now you can do:

myGameObject.name
//or
myGameObject.transform.position
//or
myGameObject.GetComponent<ScriptNameAttachedToGameObject>();

You reference stuff by putting the type, then the name after the type.

So, it looks like you want to be able to create a new wall.

Well you do that like this:

public class WallClass() {

GameObject go_Wall;
//or name it something like that I don't know...then inside of Start or Awake:

//Make sure this method is public so we can grab it and use it in another class:
public void CreateWall() {
  go_Wall = new GameObject();
  go_Wall.name = "My Wall 1";
}
}

Now from a different script you can access it like this:

public class InputOrSomething() {

public WallClass wallClass; //you can name this something else I don't know.

void Start() {
  wallClass = this.gameObject.GetComponent<WallClass>();
}

void Update() {
  if(Input.GetKey (KeyCode.Return)) {
    wallClass.CreateWall();
  }
}

If you attach that script it should create a wall if you hit the enter key. Within the CreateWall() method then you’ll want to put a bunch of information, like look up how to set that wall to be a child in your scene’s hierarchy of a certain empty ‘Walls’ object or something (for organization’s sake).

Look up how to create and attach a mesh renderer to your wall objects. Better yet, look up how to create and instantiate a prefab from your CreateWall() method.

There’s a ton of stuff you can do from there.