How do I Instantiate the same prefab at the same time from children positions?

Hi!

I haven’t found a way to make this work. The idea is as follows: The player controls a ship which has 4 empty gameObjects as children which work as “cannons”. What I want to do is to instantiate a Laser Prefab when the “Fire” key is pressed at those cannons gameObjects’ positions.

01

My code looks like this right now but it’s obviously not working:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerFire : MonoBehaviour

{

[SerializeField] GameObject playerLaserPrefab;

Vector3 playerCannons;

void Start(){

    playerCannons = gameObject.GetComponentInChildren<Transform>().position;

}

void OnFire(InputValue fireByPlayer){

    if (fireByPlayer.isPressed){

        Instantiate(playerLaserPrefab, playerCannons, Quaternion.identity);

    }

}

}

Any answers will be kindly appreciated, thank you! :slight_smile:

Hi,

Welcome to our community! :slight_smile:

Is the PlayerFire script attached to your Player game object? If so, you could iterate over the children (= the cannons) and instantiate lasers at the position of the children.


See also:

Hi Nina, good to see you once again!

Yes, the PlayerFire script is attached to my Player (parent) Game Object. What I do not know is how to get the children positions (the cannons’ positions).

Would “GetComponentsInChildren” work? Sorry, I don’t really have an idea on how to do it exactly. I did think about the iteration though and I think it will solve half of the issue!

My script looks like this. 4 Laser Prefabs ARE getting instantiated but they’re being spawned at the center of the parent Game Object, which is the Player ship. The idea is to get the Laser Prefabs instantiated at the positions of the children empty Game Objects that serve as “cannons”.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerFire : MonoBehaviour

{

    [SerializeField] GameObject playerLaserPrefab;

    [SerializeField] Vector2[] playerCannons;

   

    void OnFire(InputValue fireByPlayer){

        //Check if the player has pressed the FIRE key

        if (fireByPlayer.isPressed){

            //If they did, get the children positions through this FOR loop

            for (int iterator = 0; iterator < playerCannons.Length; iterator++){

                playerCannons[iterator] = GetComponentInChildren<Transform>().position;

                Instantiate(playerLaserPrefab, playerCannons[iterator], Quaternion.identity);

            }

        }

    }

}

Look the Transform class up in the API. The example is exactly what you need to solve your problem. And make use of Debug.Log to see what values you get. :slight_smile:

A little hint: Unity’s Find* and Get* methods return the first object they find, whatever Unity defines as “first”. If you look for a specific object and have multiple objects of the same type in your scene, those two methods are dangerous as they might return the wrong object. In the worst case, they return the expected object in the Unity Editor but the wrong object in your build.

OH MY GOD NINA, THANK YOU SOOOOO MUCH!!! Yes!!! It worked perfectly!!!

I want to show you how the code looks now!

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

public class PlayerFire : MonoBehaviour
{

    [SerializeField] GameObject playerLaserPrefab;
    

    void OnFire(InputValue fireByPlayer){

        //Check if the player has pressed the FIRE key

        if (fireByPlayer.isPressed){

            //If they did, get the children positions through this FOREACH loop and instantiate the prefab at their positions

            foreach (Transform child in transform) {

                Instantiate (playerLaserPrefab, child.transform.position, Quaternion.identity);

            }

        }

    }

}

Again, thank you very much for your guidance Nina! :smiley:

Good job! I knew you would be able to solve this problem yourself! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms