• 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 rbatelaan2 · Jun 23, 2020 at 11:39 PM · instantiatequaternionvelocityclonefor-loop

How can I spawn balls in every direction around a source (making a sphere) and then make the balls move away from that source?

I have a code that I put on my source and the ObjectToSpawn is a ball. Unity freezes when I click because something is wrong with my for loops. Also, the spheres all move in the same direction (instead of away from the source) when I don't use for loops.

 using System.Collections;
 using System.Collections.Generic;
 using System.ComponentModel.Design.Serialization;
 using UnityEngine;
 
 public class SpawnerSample : MonoBehaviour
 {
     public Rigidbody ObjectToSpawn;
     [SerializeField]
     float radius;
     void Update()
     {
         if(Input.GetMouseButton(0))
         {
             for(int angleY = 0; angleY <= 360; angleY += 45)
             {
                 for(int angleZ = 0; angleZ <= 360; angleY += 45)
                 {
                     Spawn(0, angleY, angleZ);
                 }
             }
         }
     }
 
     void Spawn(int angleX, int angleY, int angleZ)
     {
         Vector3 direction = Quaternion.Euler(0, angleY, angleZ) * Vector3.one;
         Vector3 spawnPosition = transform.position + direction * radius;
         Rigidbody Clone;
         Clone = Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity) as Rigidbody;
         Clone.velocity = direction.normalized;
     }
 }
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 UnityedWeStand · Jun 24, 2020 at 12:07 AM

You have an error in your code on line 17:

 for(int angleZ = 0; angleZ <= 360; angleY += 45)


You are incrementing angleY instead of angleZ, so angleZ never reaches the condition to terminate the for loop, thus causing Unity to freeze. I imagine you meant to write:

 for(int angleZ = 0; angleZ <= 360; angleZ += 45)


Indeed, many freezes in Unity can be attributed to infinite loops.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

162 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

Related Questions

Rotate a cloned object with key press 1 Answer

How to Instantiate and change velocity 2 Answers

How to get randomly spawning objects across a map 2 Answers

When cloning, how do you make your clones keep the original properties? 2 Answers

Instantiate with parent/gameObject's velocity 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges