Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GeoffreyFreudenberg · Aug 20, 2017 at 09:52 PM · c#scripting problem

How to add in my script that my prefabs instantiate as children of the player character who is also an instantiated prefab?

Hi there, this is my first post here so apologies if I mess something up. Below is the code I currently have for my spawner. It works, but I need the clones to become children of the player character when they spawn into the game world. The problem is, the player is also an instantiated prefab, and I'm not quite sure how to add this functionality to my script. Any help would be appreciated.

   using UnityEngine;
   using System.Collections;

    public class ChaserSpawner : MonoBehaviour
    {

 //The method of spawning (random = choose a spawn point randomly; onebyone = spawn an object on spawnpoint #1 , the next will be spawned on spawnpoint #2 ,...)
 public enum SpawnType { Random, OneByOne }

 [Header("Main parameters")]
 public SpawnType spawnType;
 public GameObject[] spawnPoints;    //The array that will contain the different spawn points
 public string toSpawnResourceName = "Chaser1";
 [Header("Extra parameters")]
 public int numberOfObjectsToSpawnOnContact = 1;
 public int maxGameobjectsToSpawn = 0;

 private int nextSpawnPointIndex = 0;
 private int spawnedObjects = 0;
 private GameObject toSpawn;

 // Use this for initialization
 void Start()
 {
     toSpawn = Resources.Load("Chaser1") as GameObject;    //Load the gameobject to spawn from resources
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (GetComponent<Collider2D>().tag == "Player") //Check for what you want about the collider here
     {
         SpawnAnObject();
     }
 }

 private void SpawnAnObject()
 {
     if (spawnedObjects >= maxGameobjectsToSpawn)    //If enough objects spawned , stop here
         return;

     for (int i = 0; i < numberOfObjectsToSpawnOnContact; i++)
     {
         GameObject spawnPoint = spawnPoints[0]; //Set a default spawnpoint to avoid errors

         if (spawnType == SpawnType.Random)
         {
             spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
         }
         else if (spawnType == SpawnType.OneByOne)
         {
             spawnPoint = spawnPoints[nextSpawnPointIndex];
             nextSpawnPointIndex++;
             if (nextSpawnPointIndex >= spawnPoints.Length)
                 nextSpawnPointIndex = 0;
         }

         Instantiate(toSpawn, spawnPoint.transform.position, Quaternion.identity);   //Spawn the wanted GameObject
         spawnedObjects++;       //Increment the number of spawned object
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unidad2pete · Aug 20, 2017 at 10:10 PM

You only need a reference to your player, and make transform of this reference parent of your spawned object, this is your code, I only added 3 new lines, with this simbol on top line / -------------------------------- /

Should be works fine

  /* -------------------------------- */
     public Transform player; // Or transform you need set parent of your spawned objects
 
     //The method of spawning (random = choose a spawn point randomly; onebyone = spawn an object on spawnpoint #1 , the next will be spawned on spawnpoint #2 ,...)
     public enum SpawnType { Random, OneByOne }
     [Header("Main parameters")]
     public SpawnType spawnType;
     public GameObject[] spawnPoints;    //The array that will contain the different spawn points
     public string toSpawnResourceName = "Chaser1";
     [Header("Extra parameters")]
     public int numberOfObjectsToSpawnOnContact = 1;
     public int maxGameobjectsToSpawn = 0;
     private int nextSpawnPointIndex = 0;
     private int spawnedObjects = 0;
     private GameObject toSpawn;
     // Use this for initialization
     void Start()
     {
         toSpawn = Resources.Load("Chaser1") as GameObject;    //Load the gameobject to spawn from resources
 
         /* -------------------------------- */
         player = GameObject.FindGameObjectWithTag("Player").transform;  //Find the player gameObject, make sure have tag named Player
     }
     void OnTriggerEnter2D(Collider2D other)
     {
         if (GetComponent<Collider2D>().tag == "Player") //Check for what you want about the collider here
         {
             SpawnAnObject();
         }
     }
     private void SpawnAnObject()
     {
         if (spawnedObjects >= maxGameobjectsToSpawn)    //If enough objects spawned , stop here
             return;
         for (int i = 0; i < numberOfObjectsToSpawnOnContact; i++)
         {
             GameObject spawnPoint = spawnPoints[0]; //Set a default spawnpoint to avoid errors
             if (spawnType == SpawnType.Random)
             {
                 spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
             }
             else if (spawnType == SpawnType.OneByOne)
             {
                 spawnPoint = spawnPoints[nextSpawnPointIndex];
                 nextSpawnPointIndex++;
                 if (nextSpawnPointIndex >= spawnPoints.Length)
                     nextSpawnPointIndex = 0;
             }
 
 
 
             GameObject ObjectSpawned =  Instantiate(toSpawn, spawnPoint.transform.position, Quaternion.identity);   //Spawn the wanted GameObject
 
             /* -------------------------------- */
             ObjectSpawned.transform.parent = player; // Set the spawned Object, child of player
 
             spawnedObjects++;       //Increment the number of spawned object
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GeoffreyFreudenberg · Aug 20, 2017 at 11:48 PM 0
Share

Hey unidad2pete, thanks a lot for the quick and helpful response! I'm not getting any errors from the modified script you gave me, but now nothing is spawning. Any idea what might be causing that to happen?

avatar image unidad2pete GeoffreyFreudenberg · Aug 20, 2017 at 11:56 PM 0
Share

Are you sure? the objects are inside of player in Hierarchy. if your prefab to be spawned have any script add a console print on start function to make sure is not spawned please.

 void Start()
     {
         print(" I spawned !! my position = " + transform.position);
     }

and one more print to check if the player transform is not null:

 print(player.gameObject.name);

after the line when instantiate the spawn for example

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

389 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i check/wait until the gameobject will end the rotation ? 1 Answer

How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer

Integer value randomly multiples itself by 3 or 2 for seemingly no reason. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges