Difference between Transform, public type-name, FindObjectOfType

Hello,

I’m having a hard time wrapping my head around all the ways to include another object or script.

I understand that Transform myObejct will create a reference to a GameObject.

Now let’s say I have a script called MyScript used in MyObject.
Then if in another GameObject I say:
public MyScript name;
Then I’ve noticed that I can drag MyObject to the newly created box but I can’t drag MyScript.

Then is it the same as public Transform myObject ? Because I know that public Component myComponent (same syntax as public MyScript name) will reference a component that is part of the same object.

And I’ve also seen that there is FindObejctOfType but so far I’ve only used it with Game Manager which seems to be special. But when I try it with something else, I can only replace by a script in my Asset (based on the choices from auto completion) but it doesn’t seem to work.

I’m confused as what they all do. And also, how am I supposed, in a gameObejct, to use a method present in a script in another gameObject

Sorry if it’s not super well explained. I got started very recently with Unity.

Thanks

Transform myObject will declare a field of type Transform. You can then fill this field with a reference to some Transform. Transform myObject isn’t the reference itself.

If you create a public MyScript name field in a MonoBehaviour, then you can drag any instance of MyScript in the scene to the field in the inspector. Note I said instance. The script file in the assets is not an instance of the component MyScript, it is the source file for the component. Instances of MonoBehaviours only exist on GameObjects.

FindObjectOfType does what it says on the tin: it attempts to find an object of the given type. If you call FindObjectOfType<MyScript>() then it will attempt to find an instance of the MyScript component in the scene. If there’s no instance in the scene, it won’t find anything. You can search for anything that subclasses
Object. This includes built-in unity components such as Rigidbody or Transform, or your own custom components such as MyScript.

The GameObject doesn’t have the method, your script does. If MyScript wants to call a method on MyOtherScript, one way to do so is to create a public MyOtherScript scriptReference; in MyScript, then drag an instance of MyOtherScript to the scriptReference field in the inspector.

Awesome, thank you!!