Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Steve Cogbill · Mar 06, 2014 at 08:03 PM · spawnparentingempty

Spawn item at different location

I have a box collider that is going to be used as a trigger and when my player enters the trigger, it spawns 1 out of 3 random items that I made. Here is the script I've been using, thanks to Dblfstr who helped create it.

 Using UnityEngine;
 Using System.Collections; //for random numbers
  
 public class itemSpawner : MonoBehaviour
 5.{
  
     bool hasSpawned = false;
    public GameObject[] itemPrefabs;
  
 10.   void OnTriggerEnter(Collider other)
    {
      if(other.tag == "Player")
      {
        if(!hasSpawned)
 15.       {
          hasSpawned = true;
          Spawn();
  
        }
 20.     }
    }
  
    void Spawn()
    {
 25. 
       Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
    }
 }

Now what I want to change is where the item spawns. It currently spawns at the location of my trigger. I want to create an empty gameObject and use that as my spawn location. Then I want to parent the spawn location to my player so that I walk through the trigger and then the item will spawn at the empty gameObject and will follow my player, since the spawn point will be parented to my player. Can I make a spot in my code that will allow me to drag an empty gameObject in the inspector onto it? I'm not sure how to add this to my code so any help would be great, thanks! Steve

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by wiiarethesound · Mar 06, 2014 at 08:17 PM

Declare a public variable first:

 public Transform spawnLocation;

And then drag a Game Object from your scene into the Inspector. Then in Spawn()

 void Spawn()
 {
     Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)], spawnLocation.position, Quaternion.identity);
 }

I'm not sure why you want to parent the spawn transform to your player though. Can you explain why please?

Comment
Add comment · Show 5 · 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 Steve Cogbill · Mar 06, 2014 at 08:28 PM 0
Share

I tried putting that in the code but it's co$$anonymous$$g up with a couple errors. The reason I want the item to spawn at the player and then follow the player is because I want the item to spawn in front of the player. And eventually I want to have the item stay in front of the player until the player decides to shoot it. So the item spawns and stays at the spawn location which is parented to the player, then the player presses the space bar and it shoots forward. Haven't got that far yet, but that's the plan.

avatar image Steve Cogbill · Mar 06, 2014 at 08:33 PM 0
Share

Fixed it actually, it should be spawnLocation not spawnTransform at the bottom

avatar image Dblfstr · Mar 06, 2014 at 08:35 PM 0
Share

I think you mean

 Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)], spawnLocation.position, Quaternion.identity);

I, too, am curious why you want to new object to follow you around. The code above to spawn at a different location will not make the item follow you, however, it will just allow you to choose a spawn location, the item will not be a child of that object by default.

So your Transform spawnLocation would need to be a child of your Player. Then after you Spawn your item, you sill need to make it a child of that gameObject.

Something like this:

  void Spawn()
 {
 GameObject item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
 item.transform.parent = spawnLocation;
 
 }
avatar image Steve Cogbill · Mar 06, 2014 at 08:43 PM 0
Share

Thanks, that worked now. Except, its spawning directly on top of my player. Can I make it sit just in front of the player?

avatar image Dblfstr · Mar 06, 2014 at 08:50 PM 0
Share

That depends on the position of the spawnLocation gameObject, and the position of your prefabs. When you drag a prefab into the scene, where is it loacted? Is it in the air, on the ground, etc. You will have to play with the values. I would set my prefab location at 0,0,0. Then it should (theoretically)spawn at the origin of the spawnLocation gameobject.

avatar image
0

Answer by Dblfstr · Mar 06, 2014 at 08:47 PM

 Using UnityEngine;
 Using System.Collections; 
  
 public class itemSpawner : MonoBehaviour
 {
 //                Variables                    //
 /* hasSpawned to check if the items trigger has */
 /* spawned an item yet, false by default */
 /* Array of your item prefabs to be spawned */
 /* these are set in the inspector */
 /* Gameobject item, this will hold your */
 /* spawned item. */
 /* Transform spawnLocation, this is where */
 /* your items will spawn. ( set in inspector) */
 /* (spawnLocation is a child of player) */
 
 bool hasSpawned = false;
 public GameObject[] itemPrefabs;
 public GameObject item;
 public Transform spawnLocation
     
     //When the player enters the trigger
     // Check that it is the player, and that
     // the trigger has not yet spawned an
     // item. If these criteria are met, call
     // the spawn function, and set
     // hasSpawned to true.
 
    void OnTriggerEnter(Collider other)
    {
       if(other.tag == "Player" && !hasSpawned)
       {
          hasSpawned = true;
          Spawn();
       }
    } 
 
     //Spawn function will randomly spawn one of your
     // item prefabs at the spawnLocation's current position
     // and rotation. This will only be called the first
     // time the trigger is entered.
 
    void Spawn()
    {
       item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],
               spawnLocation.position,
               Quaternion.identity);

       //Make the item a child of the spawnLocation GameObect.
       item.transform.parent = spawnLocation;
    }
 }
Comment
Add comment · Show 16 · 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 Steve Cogbill · Mar 06, 2014 at 08:54 PM 0
Share

Should I update my whole code to this? What are the major changes?

avatar image Dblfstr · Mar 06, 2014 at 08:57 PM 0
Share

Yes.

I created the two new variables: spawnLocation, and item.

 public GameObject item;
 public Transform spawnLocation

In the spawn function, I set item to the Instantiated prefab, so we could access its transform. Then I set it as a child of the spawnLocation.

  void Spawn()
 {
 item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],spawnLocation.position, Quaternion.identity);
 item.transform.parent = spawnLocation;
  
 }

I added a bunch of comments. And I cleaned up the trigger functtion to combine the two if statements into one statement:

 void OnTriggerEnter(Collider other)
 {
 if(other.tag == "Player" && !hasSpawned)
 {
 hasSpawned = true;
 Spawn();
 }
 } 
avatar image Dblfstr · Mar 06, 2014 at 08:58 PM 0
Share

Also, go ahead give @wiiarethesound a thumbs up for the spawnLocation.

avatar image Steve Cogbill · Mar 06, 2014 at 10:03 PM 0
Share

The objects are still spawning on my player. How do I change the values of the prefabs so they spawn in front of the player?

avatar image Dblfstr · Mar 06, 2014 at 11:28 PM 0
Share

Place the empty gameObject spawnLocation where you want them to spawn (in front of your player). Then parent the gameObejct to your player. The item will spawn where that gameObject is.

Show more comments

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

22 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

Related Questions

Creating a empty object to not lose children's scale 1 Answer

Empty Parent GameObject Moving with Child 1 Answer

spawn an object at the location of an empty gameobject 1 Answer

How to know if a collider has a rigid body inside it or not? 1 Answer

parenting a camera with a sphere 3 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