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 bradyngames · May 04, 2014 at 05:42 PM · c#gameobjects

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.

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
1
Best Answer

Answer by MikeNewall · May 04, 2014 at 06:02 PM

Try this:

         GameObject[] selectedObjects = Selection.gameObjects;
         if(selectedObjects != null){
            Debug.Log(selectedObjects[0].name);
         }
Comment
Add comment · Show 6 · 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 bradyngames · May 04, 2014 at 06:20 PM 0
Share

In trying that, I got this error:

 IndexOutOfRangeException: Array index is out of range.
avatar image clunk47 · May 04, 2014 at 10:39 PM 0
Share

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.

avatar image bradyngames · May 05, 2014 at 12:24 AM 1
Share

Checking for the Length was the correct way to go. Thanks so much, clunk47!

avatar image clunk47 · May 05, 2014 at 12:39 AM 0
Share

No problem :D

avatar image MikeNewall · May 06, 2014 at 01:28 PM 0
Share

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);
 }
Show more comments

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

23 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

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


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