How would I find and set a game object as a child in a script that isn't attached to that object

I’m working on an escape room and I can’t figure this out. I have a key that I want to get parented to my player when it is clicked on, but the script stops working around ‘void SetParent’.
My “Hand” script is attached to my player:

public class Hand: MonoBehaviour {
public Conditions GK;
public GameObject Key;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = GetComponent().ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.name == “Key”) {
if(!GK.HandFull) {
GK.HasKey=true;
GK.HandFull=true;
void SetParent(GameObject newParent)
{
Key.transform.parent = newParent.transform;
}

What do you mean by “Stops working”? Doesn’t compile or crash on runtime?
From what you pasted here it looks like you are declaring SetParent inside of Update? That does not / will never work, You can’t declare a function inside a function. Instead end the Update function, define your SetParent function after that (only inside the class) and call it from Update.

Somewhat like this:

public class Hand: MonoBehaviour
{
 public Conditions GK;
 public GameObject Key;
 void Update()
 {
  if (Input.GetMouseButtonDown(0))
  {
   Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
   Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
   RaycastHit hit;
   if (Physics.Raycast(ray, out hit))
   {
    if (hit.transform.name == "Key")
    {
     if(!GK.HandFull)
     {
      GK.HasKey=true;
      GK.HandFull=true;
      SetParent(hit.transform.gameObject)//You could use the transform directly here...
     }
    }
   }
  }
 }

 void SetParent(GameObject newParent)
 {
  Key.transform.parent = newParent.transform;
  }
}

It will cast the ray and detect that the key is being clicked, but nothing happens after that. It’s supposed to get parented to my player, but it isn’t.

try using code tags for your code.
also you haven’t replied properly to a comment about invalid code.
what does your code actually look like?

From the code you pasted, you never actually call your SetParent Function. But that’s just a wild guess, as the code you pasted should not even compile at all.

I tried defining SetParent outside of the Update function, but after changing HasKey to true, nothing happens. I don’t know if it just isn’t finding the key, or if something is still wrong with the SetParent function.

Have you checked my code, especially line 20? You will have to call your SetParent-Function. Paste your full (relevant) code again so we can take a closer look (remember to use Code-Tags).

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

public class Hand: MonoBehaviour {
public Conditions GK;
public GameObject Key;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = GetComponent().ScreenPointToRay(Input.mousePosition); //Send a raycast from the players camera to the place the mouse is pointing
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.name == “Key”) {
if(!GK.HandFull) {
GK.HasKey=true;
GK.HandFull=true; //HandFull and HasKey get set to true when ‘Key’ is clicked on
void SetParent(GameObject newParent) {
Key.transform.parent = newParent.transform; //BUG: nothing happens here
}
}
}
}
}

I also tried defining SetParent outside of Update, but got the same results

This code can not compile. If this is was the full code Unity wouldn’t start.
This is your code snippet in a working way:

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

public class Hand: MonoBehaviour {
public Conditions GK;
public GameObject Key;
void Update()
{
  if (Input.GetMouseButtonDown(0))
  {
   Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition); //Send a raycast from the players camera to the place the mouse is pointing
RaycastHit hit;
   if (Physics.Raycast(ray, out hit))
   {
    if (hit.transform.name == "Key")
    {
     if(!GK.HandFull)
     {
      GK.HasKey=true;
      GK.HandFull=true; //HandFull and HasKey get set to true when 'Key' is clicked on
      Key.transform.parent = hit.transform;//This is where the magic happens :-)
     }
    }
   }
  }
 }
}

I did not use your Function SetParent as it is one line of code anyway and put it right into the update.
Also: Line-Debug your (or use Debug.Log) to figure out what is actually happening.

I tried that bit of code, but it worked the same as it did before. The key doesn’t get parented, but everything else works. Could it be because the key is still in global space?

Just realized that there is a Unity Function called SetParent. Maybe try that.
Key.transform.SetParent(hit.transform) → Unity - Scripting API: Transform.SetParent (unity3d.com)