Adding custom properties to GameObjects

How do you guys add custom properties to gameobjects? I need to add properties that I can check. For example I want some of my GO’s to not be selectable with the mouse while I want other’s to be. So i wanted to add a selectable boolean that I could check when a GO is selected to allow it to happen or not.

Any tips?

If you are using Javascript, do something like this:

var toggleVar : boolean;

function Update() {
   if (toggleVar) {
      //do stuff
   } else {
      //don't do stuff
   }
}

That will expose the boolean variable toggleVar in the inspector. Then you just check or uncheck it and stuff happens. :slight_smile:

You’re just a virtual cornucopia of knowledge tonight! :smile:

Thanks man.

You know just a thought, I need to be able to access these “properties” in code.

For instance I’m using a raycast from the camera to select GO’s in my scene. I need to be able to say “if(GO.Selectable) then select it”.

Can I do that through this method?

Yes. Say you have a script called SelectableObject. In that script you have a variable called “isSelectable”.

Then in your raycast script, you would write:

var hit : RaycastHit;
if(Physics.Raycast(blah blah blah))
{
    var selectableComponent : SelectableObject = hit.collider.GetComponent(SelectableObject);
    if(selectableComponent  selectableComponent.isSelectable)
    {
        // select the object!
    }
}