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 /
  • Help Room /
This question was closed Sep 24, 2018 at 11:59 PM by muttsang for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by muttsang · Sep 24, 2018 at 08:11 PM · randomspawningnoobspawning problemsspawnpoints

Random.Onunitsphere confusing different spheres

Hello guys,

I am building a game where there are two planets made out of 3d Spheres. Both of them exist on a single scene. They are very small planets and i've used the theme of small planets. On the planet where the game starts , i've used Random.onUnitSphere to randomly spawn gameobjects that the player can collect. After a certain objective is completed, the player is transported to the second planet where the player also has to collect gameobjects that are spawned randomly on it's surface.There too, i tried using Random.onUnitSphere on the separate script and attached it to the second planet to spawn gameobjects but they all end up spawning on the first planet. I cannot figure out why the Random.onUnitSphere is spawning only on the first planet and not on the second. And to clarify, i've used different scripts on each planet to spawn different gameobjects. This is the code for the Script i've used on the second planet.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawnplanet2 : MonoBehaviour
 {
     public GameObject spawnPowerupPlanet2;
     //public GameObject spawnparticlePlanet2;
     // Use this for initialization
     void Start()
     {
         StartCoroutine(spawnPowerPlanet2());
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     IEnumerator spawnPowerPlanet2()
     {
         Vector3 placePlanet2 = Random.onUnitSphere * 7.4f;
         yield return new WaitForSeconds(2.0f);
          Instantiate(spawnPowerupPlanet2, placePlanet2, Quaternion.identity);
         StartCoroutine(spawnPowerPlanet2());
 
     }
 }
 

and this is the one i've used on the first planet . I've repeated some lines in this one , i'll figure a way out to reduce redundancy later, :D

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class powerupsspawn : MonoBehaviour {
 
     public GameObject spawnPowerTrap;
     public GameObject spawnPowerup;
   //  private GameObject power;
     public GameObject spawnparticle;
     public Text healthText;
     float f;
     // Use this for initialization
     void Start ()
     {
          StartCoroutine(spawnPower());
     //    StartCoroutine(SpawnandDestroy(spawnPowerup,5f));
        
     }
 
 
      IEnumerator spawnPower()
      {
          Vector3 place = Random.onUnitSphere * 7.4f;//7 is the distance on how far you want the spawn to happen
         Vector3 place2 = Random.onUnitSphere * 7.4f;
         Vector3 place3 = Random.onUnitSphere * 7.4f;
         Vector3 place4 = Random.onUnitSphere * 7.4f;
         Instantiate(spawnparticle, place, Quaternion.identity);
         Instantiate(spawnparticle, place3, Quaternion.identity);
         Instantiate(spawnparticle, place4, Quaternion.identity);
 
         Instantiate(spawnparticle, place2, Quaternion.identity);
 
         // Vector3 pickuposition = spawnPowerup.transform.position;
 
         //yield return new WaitForSeconds(3.5f);
         yield return new WaitForSeconds(2.0f);
         GameObject spawned = Instantiate(spawnPowerup, place, Quaternion.identity);//the position takes a vector3 value so "place" is a random vector 
         GameObject spawned1 = Instantiate(spawnPowerup, place2, Quaternion.identity);
         GameObject spawnedTrap2 = Instantiate(spawnPowerTrap, place3, Quaternion.identity);
         //  GameObject spawnedTrap = Instantiate(spawnPowerTrap, place2, Quaternion.identity);
         GameObject spawnedTrap = Instantiate(spawnPowerTrap, place4, Quaternion.identity);
         Destroy(spawned,1.5f);//destroys spawned gameobject sater 3 seconds
         Destroy(spawned1, 1.5f);
         Destroy(spawnedTrap2, 4f);
         
         Destroy(spawnedTrap, 4f);
 
       
         
       
        StartCoroutine(spawnPower());//loops the spawning
 
     
 
      }
 
   
     // Update is called once per frame
     void Update () {
         healthText.text = "Health :" + GameObject.Find("Player").GetComponent<PlayerMovementScript>().health;
     }
 }
 
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

  • Sort: 
avatar image
0
Best Answer

Answer by muttsang · Sep 24, 2018 at 11:59 PM

no problem folks :D I found a solution i used this i.e added the position of the second planet from the script .

  Vector3 placePlanet2 = gameObject.transform.position + Random.onUnitSphere * 7.4f;

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

159 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

Related Questions

Enemy Spawner 2 Answers

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

How do I stop enemy from spawning on start? 1 Answer

Randomly Spawn Outside The Camera Field of View 1 Answer

Making an object spawned from a list became a child of the player? 0 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