I’m following a tutorial from the Walker Boys, specifically this tutorial: 3 02 Tool Prefab Creator Selection Array on Vimeo
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.