Do you have to use GetComponent?

Im a bit confused on how to mimick behavior like transform.

That is i know that if i make a script and i make the integers public and i make the
functions public i can just type

script.function();
or
script.variable = x;

But it seems making a class public doesnt unlock that same functionality (i have to use get component)

I have 2 game objects

torpedoTube
torpedo

torpedo Tube spawns an instance of the prefab torpedo and saves it as a variable
now torpedo prefab has the script torpedoBehavior attached to it in the prefab to ensure
all torpedos come equipped with there behavior scripts.

However i can’t seem to do

MyTorpedo = instantiate(torpedo,…,…);

MyTorpedo.TorpedoBehavior.Fire();

I ahve to use getComponent.

I feel like if the component is there it’s attached it should just be an option to type.

I understand under the hood it would go and find the component. It’s the same thing in the end I get that.

But syntax wise I don’t understand why public variables of public classes can just go
class.variable
but i cant go

gameobject.class

Especially since for common classes (transform)
the behavior already exists.

Is there a way to do this?
Since classes are public be default and thats the only reason i could see for not seeing them i’m not sure.

You are misunderstanding what is going on.

GameObject is itself a class. Adding a component to it will not change the definition of the class. The reason you can do GameObject.transform is because transform is defined in GameObject (and actually just wraps a GetComponent call if I recall correctly).

The GameObject class is holding references internally to the instances of classes attached to it and you are accessing them via methods on the GameObject class. You are not altering the GameObject class definition at all.

Imagine a Dictionary. You can do Dictionary.Add(“hello”, null), but this will not allow you to access the string with Dictionary.hello. You can access it via Dictionary[“hello”], but that is using an indexer, which is something different again. The reason (probably) the GameObject class doesn’t implement an indexer is because it would not allow you to add two instances of the same component, and also be slow (strings = bad)

On a side note, I think c# won’t allow you to override the . operator.

transform is a object/component attached to a gameObject. gameObject.transform is not a class, it is a instance of the Transform class. A class declared as public doesn’t make it accessible by the way you describe it.

public class ComponentA : MonoBehaviour {
   //....
}

public class MyGameObject : MonoBehaviour {
   public ComponentA componentA;
   
   //...
}

Only by doing this you will get:

public MyGameObject mgo;

....

mgo.componentA.DoSomething();

Back to your code, if you have attached the torpedoBehaviour to your torpedo gameObject, then:

TorpedoBehaviour tb = Instantiate( torpedo ... ) as TorpedoBehaviour;

tb.Fire();