Accessing data from the owner(prefab) of an instantiated object(prefab)?

I need to be able to grab the direction in which the instantiator(is that a word?) is facing, within the object that’s been instantiated.

Maybe there’s an easier way to do this…

I have an enemy prefab that shoots a projectile (another prefab). I need to be able to tell the instantiated projectiles which direction they need to be traveling, based on which direction the object that instantiated it was facing at the time of instantiation… if that makes sense.

Is there a way to pass that data on to the newly instantiated object?

Very easily yes…

var instance = Instantiate(prefab, somePosition, transform.rotation);
instance.transform.rotation = transform.rotation;

Or:

Instantiate(prefab, somePosition, transform.rotation);
1 Like

I probably should have specified a bit further…

Rotation isn’t actually the data I need to pass in (I shouldn’t have said direction…). This is for a 2D game with enemies facing either left or right, so I need to get the transform’s X scale, so in my case it’d be a -1 or a 1 (Transform.localScale.x ?). Is that something I can pass in just as the rotation can be, or do I need to do this another way?

The concept is the same. Grab the return value of Instantiate (the instance) and do whatever you want with it.

2 Likes

Ah, I see now. Thanks :wink:

This is a little embarrassing. I have to admit that I am still confused. I thought that I understood the above, but I’m having more trouble than I anticipated…

Alright so just to lay the logic out here (for my own benefit), ClassA is attached to the enemy prefab. ClassA instantiates the projectile prefab, which ClassB is attached to. ClassB needs to know the local scale X from ClassA. ClassB can use the instance from ClassA, is that correct? Since ClassB wouldn’t be a child of ClassA’s game object, is there a way to get a reference to that specific ClassA from that specific ClassB?

What confuses me even further is if there are multiple enemy prefabs present. Let’s say that they all instantiate the projectile prefab at the same time. How does a specific ClassB know which specific ClassA it should be using for the local scale X value?

Am I thinking about this correctly?

Let’s see if this clears things up. When you instantiate something you get a copy, distinct from the original. Hence the first parameter of Instantiate being UnityEngine.Object original.

So when you instantiate a prefab you get distinct duplicates, all with their own distinct lives, hopes and dreams regardless of being ‘the same’. Each component on said prefab, as you would gather by now, are all distinct from the prefabs on every other prefab based upon the same original.

So when an instance of a prefab instantiates another prefab, caches the return value (the instance), and passes in values from itself, those values come from that distinct instance. 100 instances firing off 100 projectiles will all be communicating between themselves and the profiles they instantiated.

1 Like

This does help me to understand what’s happening under the hood, so I appreciate that.

I’d like to try and understand the code side of things a bit more, though -

Taking PraetorBlue’s example above -

    var instance = Instantiate(prefab, somePosition, transform.rotation);
    instance.transform.rotation = transform.rotation;

Let me see if I can understand how to implement this (I’m not new to programming or C# in general, but I’m old, so it takes me a little longer to fully grasp some concepts)…

Alright, so the above would be in ClassA. Once ClassB was instantiated, how would I then grab the reference to ClassA so that I can use that instance?

Normally, and let’s say for the sake of argument that the instantiated prefab was now a child of the object that instantiated it, I would do something like this -

public ClassB
{
  ClassA classA;
  void Start()
  {
    classA = GetComponentInParent<ClassA>();
  }

  void Update()
  {
    classA.instance... //do something with this
  }
}

However, since it isn’t a child of ClassA, I can’t do that. Is there another way of grabbing a reference to ClassA?

My example is showing exactly how to do it. My use of var actually obfuscates things a little. So let me show you two examples. Let’s assume Class B looks like this:

public class B : MonoBehaviour {
  public A refToA;
}

Then there’s two ways to do this. First, let’s assume your prefab is of type GameObject. Then you can do something like this:

public class A : MonoBehaviour {
  public GameObject prefab;

  void Spawn() {
    GameObject instance = Instantiate(prefab);
    B bInstance = instance.GetComponent<B>();
    bInstance.refToA = this;
  }
}

But there’s a simpler way - you can make the prefab actually of type B and skip the rigamarole of GetComponent etc:

public class B : MonoBehaviour {
  public A refToA;
}

public class A : MonoBehaviour {
  public B prefab;

  void Spawn() {
    B bInstance = Instantiate(prefab);
    bInstance.refToA = this;
  }
}
1 Like

OK, so this part really helped me to understand what’s happening here -

    public class B : MonoBehaviour {
      public A refToA;
    }
   
    public class A : MonoBehaviour {
      public B prefab;
   
      void Spawn() {
        B bInstance = Instantiate(prefab);
        bInstance.refToA = this;
      }
    }

Awesome. Thank you both for taking the time to explain this to me. I don’t think I’ve ever done anything like this before, but there’s a lot of C# and programming that’s still a mystery to me :wink: