Access a separate gameobject's component's properties via script?

I’ve recently tried to get back into Unity, and for now, want to spawn a “hook” for a spring joint when the player touches the floor for a movement mechanic, but I’m having trouble enabling the spring in the first place.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HookSpawner : MonoBehaviour //In a script parented to the floor
{
    public GameObject Player; //I pass the gameobject for the player through

    void OnCollisionEnter2D(Collision2D other)//And on collision with eachother
    {
        Debug.Log("Pixel has touched a floor tile");
        Player.GetComponent<SpringJoint2D>().Enable(true);//*   The floor SHOULD spawn the hook
    }
}

Everywhere online tells me something like this should work just fine, but the last line* throws an error that “SpringJoint2D” does not contain a definition for “Enable”. So It’s looking for an “Enable” method, not an “Enable” property.

But when I google that I only see guides on how to enable a property for a component within the same gameobject…

That would be because you are trying to call a method named Enable and passing true as a parameter.
Maybe you are wanting to do this:
Player.GetComponent<SpringJoint2D>().enabled = true;
Which would set the enabled property to true.

1 Like

To add to the above, the easiest to solve these kinds of problems is simply to refer to the documentation: Unity - Scripting API: SpringJoint2D

1 Like

Thank you, it worked! And, apologies for the silly question, my most recent attempts at games have come from Roblox Studio, where you can move up and down the hierarchy with just a period, so scripts like LocalScript.LocalGameObject.Scene.OtherGameObject.OtherScript.enabled = true; can exist.