- Home /
How would you convert this JavaScript code into C#?
I'm following a tutorial from the Walker Boys, specifically this tutorial: http://vimeo.com/album/1546437/video/20685640
This is the code that I have been trying to convert (which is all inside a static void MakePrefabs function):
var selectedObject : GameObject[] = Selection.gameObjects;
Debug.Log(selectedObject[0].name);
I will provide all the examples that I have tried along with the errors they generate in that order. Straight code conversion:
GameObject[] selectedObject = Selection.gameObjects;
Debug.Log(selectedObject[0].name);
IndexOutOfRangeException: Array index is out of range.
This makes sense due to the fact that I didn't specify the amount of indexes I wanted in the array. Specifying the array index amount:
GameObject[] selectedObject = new GameObject[1];
Debug.Log(selectedObject[0].name);
NullReferenceException: Object reference not set to an instance of an object
This shows that I didn't specify the fact that I wanted to grab the selected gameObjects. In my attempts to do this, I have tried to use both temp variables and lists; I will provide both examples. Using a temp variable:
GameObject[] selectedObject = new GameObject[1];
GameObject[] _tmp = Selection.gameObjects;
_tmp = selectedObject;
Debug.Log(selectedObject[0].name)
NullReferenceException: Object reference not set to an instance of an object.
This is also not grabbing the gameObjects. I have also tried it this way:
GameObject[] selectedObject = new GameObject[1];
GameObject[] _tmp = Selection.gameObjects;
selectedObject = _tmp;
Debug.Log(selectedObject[0].name);
IndexOutOfRangeException: Array index is out of range.
This is the attempt using a List:
List<GameObject> selectedObject = new List<GameObject>();
selectedObject.Add(new GameObject());
Debug.Log(selectedObject[0].name);
This "works" as it creates a new GameObject() and returns it; however, I only want to return the game objects that are already in the Hierarchy which at this point, is a Cube object.
TL;DR
Here is the full script and all the attempts that I have tried to convert this code in from JavaScript to C#:
// JS code that I'm trying to convert
var selectedObject : GameObject[] = Selection.gameObjects;
Debug.Log(selectedObject[0].name);
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class MakePrefab : MonoBehaviour {
[MenuItem ("Project Tools/Make Prefabs #%&_p")]
static void MakePrefabs() {
// Attempt 1
GameObject[] selectedObject = Selection.gameObjects;
Debug.Log(selectedObject[0].name);
// Attempt 2
GameObject[] selectedObject = new GameObject[1];
Debug.Log(selectedObject[0].name);
// Attempt 3
GameObject[] selectedObject = new GameObject[1];
GameObject[] _tmp = Selection.gameObjects;
selectedObject = _tmp;
Debug.Log(selectedObject[0].name);
// Attempt 4
List<GameObject> selectedObject = new List<GameObject>();
selectedObject.Add(new GameObject());
Debug.Log(selectedObject[0].name);
}
}
Any input or tips would be GREATLY appreciated as I have been trying to figure this out for a long time now. Thank you for your time.
Answer by MikeNewall · May 04, 2014 at 06:02 PM
Try this:
GameObject[] selectedObjects = Selection.gameObjects;
if(selectedObjects != null){
Debug.Log(selectedObjects[0].name);
}
In trying that, I got this error:
IndexOutOfRangeException: Array index is out of range.
Ins$$anonymous$$d of checking if the array is null, check to see if Length is greater than zero.
if(selectedObjects.Length > 0)
{
Debug.Log(selectedObjects[0].name);
}
To avoid the index out of range exception.
Checking for the Length was the correct way to go. Thanks so much, clunk47!
You don't need to store the objects in a local variable either. You can do this, which is more succinct :)
if(Selection.gameObjects.Length > 0)
{
Debug.Log(Selection.gameObjects[0].name);
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# GameObject Lists 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Restricition of Objects. 1 Answer