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 crimi · Jan 20, 2013 at 10:14 AM · randomspheresurface

Random Prefab around Sphere convert C# to Javascript

Hi mates,

I got a small C# snippet what i would like to convert to Js.

What is the script doing? It instantiate for example 10 Cubes randomly around a sphere.

I started already to convert it to Java, but still got lots of errors. There must be no context menu, it could work also just on Start. Would be nice if someone is able to help me. I already was looking for converter tools.

Here the C#:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class PointsOnSphere : MonoBehaviour {
     public GameObject prefab;
     public int count = 10;
     public float size = 20;
     
     [ContextMenu("Create Points")]
     void Create () {
         var points = UniformPointsOnSphere(count, size);
         for(var i=0; i<count; i++) {
             var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity) as GameObject;
             g.transform.parent = transform;
         }
     }
     
     Vector3[] UniformPointsOnSphere(float N, float scale) {
         var points = new List<Vector3>();
         var i = Mathf.PI * (3 - Mathf.Sqrt(5));
         var o = 2 / N;
         for(var k=0; k<N; k++) {
             var y = k * o - 1 + (o / 2);
             var r = Mathf.Sqrt(1 - y*y);
             var phi = k * i;
             points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
         }
         return points.ToArray();
     }
 }

Here my Java so far.

 var prefab : GameObject;
 
 var count : int = 10;
 
 var size : float = 20;
 
 var points : Array;
 
    
     function Start () {
         var points = UniformPointsOnSphere(count, size);
         for(var i=0; i<count; i++) {
             var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity);
             g.transform.parent = transform;
         }
     }
     
     function UniformPointsOnSphere ( N : float , scale : float  ) : Vector3[] {
         var points =  new Array (Vector3(0, 0, 0));
         var i = Mathf.PI * (3 - Mathf.Sqrt(5));
         var o = 2 / N;
         for(var k=0; k<N; k++) {
         var y = k * o - 1 + (o / 2);
         var r = Mathf.Sqrt(1 - y*y);
         var phi = k * i;
         points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
         }
         return points.Array;
     }
Comment
Add comment · Show 5
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 flaviusxvii · Jan 20, 2013 at 03:14 PM 0
Share

Java is an actual language that is very different than javascript. They are unfortunately named.

avatar image Vonni · Jan 20, 2013 at 03:18 PM 0
Share

And this gives an error? Looks fine to me, just cast all your variables. What errors are you getting?

avatar image crimi · Jan 20, 2013 at 03:27 PM 0
Share

Yeah i mean javascript.

Get this Error now. "$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityScript.Lang.Array.ToArray'.

avatar image flaviusxvii · Jan 20, 2013 at 07:39 PM 0
Share

Well "points" is an array, so you don't need to call "ToArray()".

avatar image crimi · Jan 21, 2013 at 02:03 PM 0
Share

I did updated the code above. Hope "return points.Array;" is right.

Now i got this error.

NullReferenceException: Object reference not set to an instance of an object

@this line "var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity);"

So must be +points[i] ....

Does anyone see something what is really wrong? Im not a C# expert :=)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by crimi · Jan 22, 2013 at 09:41 PM

I did giving up to convert the script and started from scratch, so here is my solution. I also wanted to face the cube the center.

alt text

 var thePrefab : GameObject; 
 var count : int = 10; // Number of cubes
 var sphereSize : float = 10; // Radius of sphere
 
 function Start () {
     var center = transform.position;
     for (var i=0; i < count; ++i) {
         yield WaitForSeconds (0.5); // Spawning delay
        var pos = Random.onUnitSphere * sphereSize;
         var rot = Quaternion.FromToRotation(Vector3.forward, center-pos); // Face cubes to center
         Instantiate(thePrefab, pos, rot);
     }
 }

random_cubes_around_sphere.png (21.2 kB)
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
avatar image
0

Answer by Vonni · Jan 20, 2013 at 03:19 PM

Missing

using System.Collections.Generic;

!

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Randomly Instantiating an object inside a shrinking sphere? 1 Answer

Instantiate a Prefab along the surface of another GameObject (Sphere) 1 Answer

Moving rigidbodies at random 1 Answer

Problems with an object rotating a sphere surface (Quaternions and LookAt) 0 Answers

[C#] Algorithm for randomly packing random radius sphere in fixed radius sphere. 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