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
1
Question by yenotsuns · May 31, 2020 at 09:41 PM · collisionprefabspawnspawning problemsrestart

Spawn different gameObjects without using a prefab? | Spawned gameObjects don't enable restartPanel

Hello! A desperate beginner here.

I am making a game where different gameObjects are spawned and you have to drag them to prevent them from collision. Upon collision a restartPanel is enabled. This system works fine with gameObjects that are already placed in the game (hierarchy), but the collision of spawned gameObjects does not enable the restartPanel (because dragging a scene on a prefab is not possible, as I just learned).

Is there a way to spawn different gameObjects without using a prefab?


For spawning I used this code with a prefab and multiple sprites

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HumanSpawn : MonoBehaviour
 {
 
     public GameObject humanPrefab;
     public Sprite[] humanSprites;
 
     float randX;
     Vector2 whereToSpawn;
     public float spawnRate = 2f;
     float nextSpawn = 0.1f;
 
   
     void Update()
     {
         if (Time.time > nextSpawn){
 
             nextSpawn= Time.time + spawnRate;
             randX = Random.Range (-8.4f, 8.4f);
             whereToSpawn = new Vector2 (randX, transform.position.x);
             Instantiate(humanPrefab, whereToSpawn, Quaternion.identity);
         }
     }
 }







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
1
Best Answer

Answer by Hellium · May 31, 2020 at 09:55 PM

Prefabs is the way to go. If you want to reference your restartPanel in a script attached to your prefabs, your best option is to inject the reference when the prefab gets instantiated.

Supposing you have a script called Human attached to your prefab, you can do something like this:

 public class Human : MonoBehaviour
 {
     public GameObject RestartPanel { get; set; }

     private void OnCollisionEnter(Collision collision)
     {
          RestartPanel.SetActive(true);
      }
 }

Then, in your spawner script:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class HumanSpawn : MonoBehaviour
  {
      // Drag & drop the restartPanel in the inspector
      // You should be able to since `HumanSpawn` is most likely in your scene already
      public GameObject restartPanel;
      public GameObject humanPrefab;
      public Sprite[] humanSprites;
  
      public float spawnRate = 2f;
      float nextSpawn = 0.1f;
  
    
      void Update()
      {
          if (Time.time > nextSpawn)
         { 
              nextSpawn = Time.time + spawnRate;
              float randX = Random.Range (-8.4f, 8.4f);
              Vector2 spawnPosition = new Vector2 (randX, transform.position.x); // Don't you mean transform.position.y?
              GameObject human = Instantiate(humanPrefab, spawnPosition, Quaternion.identity);
              human.GetComponent<Human>().RestartPanel = restartPanel;
          }
      }
  }

Comment
Add comment · Show 3 · 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 yenotsuns · Jun 01, 2020 at 06:51 PM 0
Share

Yes, thank you so much for taking the time to type this out! Referencing the restartPanel in the prefab did the trick! :) For some reason, after the changes in the script, only one of the sprites is spawning. Would using a switch statement fix this?

avatar image Hellium yenotsuns · Jun 01, 2020 at 07:19 PM 1
Share

I am not sure you would have different sprites with the code you have provided. Try something like this:

   void Update()
   {
       if (Time.time > nextSpawn)
       { 
           SpawnHuman();
           nextSpawn = Time.time + spawnRate;
       }
   }

  void SpawnHuman()
  {
       float randX = Random.Range (-8.4f, 8.4f);
       Vector2 spawnPosition = new Vector2 (randX, transform.position.x); // Don't you mean transform.position.y?
       GameObject human = Instantiate(humanPrefab, spawnPosition, Quaternion.identity);
       human.GetComponent<Human>().RestartPanel = restartPanel;
       human.GetComponent<SpriteRenderer>().sprite = humanSprites[Random.Range(0, humanSprites.length)];
  }
avatar image yenotsuns Hellium · Jun 01, 2020 at 09:05 PM 0
Share

YOU ARE A SAVIOUR, thank you!

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

224 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

Related Questions

Spawn gap in unity? 0 Answers

Error: Instantiated Enemies don't get hit 2 Answers

Destroy a GameObject 2 Answers

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Coin collecting with prefabs 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