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
-1
Question by JulioKoebes · Jun 10, 2013 at 02:02 PM · c#javascriptconversion

js conversion to c#. no errors but still not working

Hello

Im new to unity, and in never worked with js. There was a pretty good tutorial about a PickupSpawner which i wanted to use... But i needed the code in c# because the rest of my project is in c# and i'm not used to js.

so here is the original js code:

 #pragma strict
 // minimum and maximum spawn delay times
 public var minimumSpawnDelayTime:int = 1;
 public var maximumSpawnDelayTime:int = 5;
 
 // the pickup prefab, assigned via the Inspector
 public var pickupPrefab:GameObject;
 public var pickupPrefab2:GameObject;
 public var pickupPrefab3:GameObject;
 public var pickupPrefab4:GameObject;
 
 // the number of pickups to have
 public var numberOfPickups:int = 2;
 
 // the ARRAY of spawnpoints that our pickup will be spawned at
 private var spawnPointList:GameObject[];
 
 // array of which spawn points are available
 private var spawnIndexAvailableList:Array = [];
 
 // variable to hold the total number of spawn points, saves having to recalculate
 private var numberOfSpawnPoints:int;
 
 public var destroyed = false;
 
 function Awake()
 {
     // retrieve GameObjects tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
     spawnPointList = gameObject.FindGameObjectsWithTag("SpawnPoint");
 
     // retreive number of spawn points
     numberOfSpawnPoints = spawnPointList.length;
     
     // make sure number of pickups doesn't exceed number of spawn points
     if (numberOfPickups > numberOfSpawnPoints) numberOfPickups = numberOfSpawnPoints;
     
     // make all spawn points available by setting each index to true
     for (var i:int = 0;  i < numberOfSpawnPoints; i++) 
     {
         spawnIndexAvailableList[i] = true;
     }
     
     // spawn X amount of pickups according to numberOfPickups
     for (var j:int = 0;  j < numberOfPickups; j++)  
     {
         SpawnPickup();
     }
     
 }
 
 function SpawnPickup()
 {
     // generate a random integer to use as the index to select a spawn point from the list
     var randomSpawnIndex:int = Random.Range(0, numberOfSpawnPoints);
     
     // while the selected spawn index is unavailable regenerate another one
     while (!spawnIndexAvailableList[randomSpawnIndex])
     {
         randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
     }
     
     // retrieve the position and rotation of the pickups spawn point
     var spawnedPickupPosition:Vector3 = spawnPointList[randomSpawnIndex].transform.position;
     //var spawnedPickupRotation:Quaternion = spawnPointList[randomSpawnIndex].transform.rotation;
     
     // instantiate (create) the pickup prefab with the above position and rotation
     var x = Random.Range(1,10);
     //var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);
     var spawnedPickup:GameObject;
     
     if (x <= 5)
     {
     spawnedPickup = Instantiate(pickupPrefab, spawnedPickupPosition, Quaternion.identity);
     }
     else
     {
     spawnedPickup = Instantiate(pickupPrefab2, spawnedPickupPosition, Quaternion.identity);
     }
 
     // set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of
     // this is so we can use SendMessageUpwards within scripts attached to the pickupPrefab to call functions within this script
     spawnedPickup.transform.parent = spawnPointList[randomSpawnIndex].transform;
 
     
     // set the name of the pickup as its index
     spawnedPickup.name = randomSpawnIndex.ToString();
 
     
     // make the spawn index unavailable to prevent another pickup being spawned in this position
     spawnIndexAvailableList[randomSpawnIndex] = false;
 }
 

This is what i converted it to:

c#:

 using UnityEngine;
 using System.Collections;
 
 public class PickupSpawner : MonoBehaviour {
     
 // minimum and maximum spawn delay times
 public int minimumSpawnDelayTime = 1;
 public int maximumSpawnDelayTime = 5;
 
 // the pickup prefab, assigned via the Inspector
 public  GameObject pickupPrefab1;
 public  GameObject pickupPrefab2;
 public  GameObject pickupPrefab3;
 public  GameObject pickupPrefab4;
 
 // the number of pickups to have
 public int numberOfPickups = 2;
 
 // the ARRAY of spawnpoints that our pickup will be spawned at
 private GameObject[] spawnPointList;
 
 // array of which spawn points are available
 private bool[] spawnIndexAvailableList;
 
 // variable to hold the total number of spawn points, saves having to recalculate
 private int numberOfSpawnPoints;
     
     // Use this for initialization
     void Start () {
         
     // retrieve GameObjects tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
     spawnPointList = GameObject.FindGameObjectsWithTag("SpawnPoint");
 
     // retreive number of spawn points
     numberOfSpawnPoints = spawnPointList.Length;
     
     // make sure number of pickups doesn't exceed number of spawn points
     if (numberOfPickups > numberOfSpawnPoints) numberOfPickups = numberOfSpawnPoints;
     
     // make all spawn points available by setting each index to true
     for (int i = 0;  i < numberOfSpawnPoints; i++) 
     {
         spawnIndexAvailableList[i] = true;
     }
     
     // spawn X amount of pickups according to numberOfPickups
     for (int j = 0;  j <= numberOfPickups; j++)  
     {
         SpawnPickup();
     }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void SpawnPickup()
     {
     // generate a random integer to use as the index to select a spawn point from the list
     int randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
     
     // while the selected spawn index is unavailable regenerate another one
     while (!spawnIndexAvailableList[randomSpawnIndex])
     {
         randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
     }
     
     // retrieve the position and rotation of the pickups spawn point
     Vector3 spawnedPickupPosition = spawnPointList[randomSpawnIndex].transform.position;
     
     // instantiate (create) the pickup prefab with the above position and rotation
     var x = Random.Range(1,10);
     //var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);
     GameObject spawnedPickup;
     
     if (x <= 5)
     {
     spawnedPickup = Instantiate(pickupPrefab1, spawnedPickupPosition, Quaternion.identity) as GameObject;
     }
     else
     {
     spawnedPickup = Instantiate(pickupPrefab2, spawnedPickupPosition, Quaternion.identity) as GameObject;
     }
 
     // set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of
     // this is so we can use SendMessageUpwards within scripts attached to the pickupPrefab to call functions within this script
     spawnedPickup.transform.parent = spawnPointList[randomSpawnIndex].transform;
 
     
     // set the name of the pickup as its index
     spawnedPickup.name = randomSpawnIndex.ToString();
 
     
     // make the spawn index unavailable to prevent another pickup being spawned in this position
     spawnIndexAvailableList[randomSpawnIndex] = false;
 }

There are no compiling errors, but still it doesnt work. I assigned everything as it was done in the tutorial...

There is one warning in line 23: private bool[] spawnIndexAvailableList; Field PickupSpawner.spawnIndexAvailabeList is never assigned to, and will always it default value 'null' What does it mean exactly?

Anyone any ideas?

greetings

Comment
Add comment · Show 3
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 robertbu · Jun 10, 2013 at 02:21 PM 0
Share

Other than @Chronos-L's catch the only other difference I see is the use of Start() rather than Awake() (which should not be a functional difference). Code like this is ideal to debug in the monodevelop debugger since it only runs once and the indices of the loops are small:

http://unitygems.com/debugging-game-monodevelop/

avatar image JulioKoebes · Jun 10, 2013 at 02:50 PM 0
Share

Thanks you two

Now

I debugged it just in that way and everything works fine until here: for (int i = 0; i < numberOfSpawnPoints; i++) { spawnIndexAvailableList[i] = true; }

each value in the spawnIndexAvailableList array is succesfully set to true, but then when it hits the end of the array it crashes...

any idea?

avatar image JulioKoebes · Jun 10, 2013 at 03:00 PM 0
Share

it worked. thank you guys!

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Chronos-L · Jun 10, 2013 at 02:10 PM

"There are no compiling errors", but I think the C# version of the code might produce a runtime error since you do not initialize the spawnIndexAvailableList.

Before Line 40, add this line spawnIndexAvailableList = new bool[numberOfSpawnPoints];, I think that is the only thing I can add to the C# code. Other than that, I do not see any difference between the JS and C#.

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

15 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

Related Questions

Some Basic Conversions from JavaScript to C# 1 Answer

Integer to Character in unityscript 2 Answers

Converting a JS script into C# - hit a wall with arrays... 1 Answer

Help with conversion from javascript to c# 3 Answers

Multiple Cars not working 1 Answer


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