How do I Instantiate something and make the created object follow the creator?

Hi I’d like to have a situation like this:

On the player’s Start, the player creates a force field.

On the player’s Update the force field is moved to the player’s position.

You could parent the forcefield to the player, that way you don’t need to worry about updating its position.

Yes I could, but how do I do it the way I described in the opening post?

public Transform thingToFollow;

void LateUpdate()
{
  transform.position = thingToFollow.position;
}

Another way to accomplish this is by using joints.

This is the path to the dark side, but there is also Unity’s Script Execution Order settings.

Here’s another way. Closer to what you wanted.

public class Player : MonoBehaviour
{
  public ForceField forceFieldPrefab;

  void Start()
  {
    forceField = Instantiate(forceFieldPrefab, transform.position, transform.rotation) as ForceField;
  }

  void Update()
  {
    // Do stuff to make the player move.
    // Now update the force field position
    forceField.GoTo(transform.position);
  }

  ForceField forceField { get; set; }
}
public class ForceField : MonoBehaviour
{
  public GoTo(Vector3 worldPosition)
  {
    transform.position = worldPosition;
  }
}

The suggested way. This way you can have your force field as a prefab (much desired) and the force field will always be in the right position. BONUS: It will even rotate and scale with the player!!!

public class Player : MonoBehaviour
{
  public ForceField forceFieldPrefab;

  void Start()
  {
    forceField = Instantiate(forceFieldPrefab, transform.position, transform.rotation) as ForceField;
    // Attach forceField as a child.
    forceField.transform.parent = transform;
  }

  ForceField forceField { get; set; }
}

It sounds really good. But when I try it I get an error on the on player.start:
Object referenced not set to an instance of an object.

Let’s try this. When you put this script on a GameObject, and then select that GameObject, you should see a slot in the Unity Inspector called Force Field Prefab. You need to drag your prefab into that slot.

using UnityEngine;

public class Player : MonoBehaviour
{
  // Drag your force field prefab into the inspector here.
  public GameObject forceFieldPrefab;

  void Start()
  {
    GameObject forceField = Instantiate(forceFieldPrefab, transform.position, transform.rotation) as GameObject;
    if (forceField == null)
      Debug.LogWarning("No prefab to instantiate! Assign in the Inspector.");
    else
      forceField.transform.parent = transform;
  }
}

Thanks for being patient with me! And thanks for actually giving me code to work with. Many times I get on this forum people just tell me to make my own code. But I can’t learn that way, I only learn from dissenting things that I can test that work.

I managed to shorten down your code to get the same effect to:

GameObject MyMod= Instantiate(glob.OB_CHSAK,transform.position,transform.rotation) as GameObject;
    MyMod.transform.parent = transform;

Where glob.OB_CHSAK is the force field object stated as:

public static GameObject OB_CHSAK=(GameObject)Resources.Load("Mod/chSak");

In a script that I have that gets called by no object but has a lot of public variables.

1 Like

Exactly what @Beennn1 was suggesting. :wink:

1 Like