Home > Net >  Spawned objects don't show on the surface of the position i wanted them to show
Spawned objects don't show on the surface of the position i wanted them to show

Time:07-26

I have a script where i click and spawn objects out of the bushes. the problem is when the Item is spawned which is shown in the heirarchy as (Clone) doesn't show in play mode can't see it anywhere.

this is the picture of the play mode:

Spawning objects do not show in game

and this is the script for the spawning objects i don't know if i'm doing something wrong in randomizing the position:

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

public class RandomObjectSpawner : MonoBehaviour
{

    public GameObject[] myObjects;



    void Update()
    {
        if (Input.GetMouseButtonDown(0))

        {

            int randomIndex = Random.Range(0, myObjects.Length - 1);
            Vector3 randomSpawnPosition = new Vector3(Random.Range(-10, 11), 5, Random.Range(-10, 11));

            Instantiate(myObjects[randomIndex], randomSpawnPosition, Quaternion.identity);
             
        }


    }
}

CodePudding user response:

I'm assuming that the spawned objects are also Canvas Objects. So you have to set the parent to somewhere inside the canvas.

This can be done directly with the instantiate method. You can add the transform of the parent as a fourth parameter for your Instantiate method.

To do that add public Transform groupTransform; in your RandomObjectSpawner class. Change your Instantiate line to:

Instantiate(myObjects[randomIndex], randomSpawnPosition, Quaternion.identity, groupTransform);

Then in the Editor drag your Group Gameobject to the groupTransform field on the RandomObjectSpawner script.

  • Related