Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by daviddickball · Dec 31, 2015 at 01:09 AM · uimenu screenmultidimensional arrayobject-reference-errorselections

Getting 'object reference not set' after deactivating objects & activating them

alt text

I'm building a character selection screen for 2 players to select from a choice of 3 characters. I present the GameObjects in the interface (named Player1A, Player1B, Player2A etc) and want to disable/enable them when selected. I haven't got very far, I'm stuck on hiding them all initially, where I loop through them all and unhide the ones I want initially selected.

But I get the error "Object reference not set to an instance of an object" when finding the object a 2nd time. What should I be doing in this case, using a multidimensional array to store references to the gameObjects? That seems complex to me (I'm not such a strong programmer!). Is there an easier way to do what I'm doing? Any tips would be useful. Thanks! Here's my code:

 //--create array of the possible different player characters 
 public var playerCharacters = new Array ("A", "B");
 private var numPlayers : int = 2;
 
 function Start () {
     
     //--hide all the characters, then show the 1st ones
     for(var i : int = 1; i <= numPlayers; i++){
         
         for(var j : int = 0; j < playerCharacters.length; j++){
             Debug.Log("hide Player"+i+playerCharacters[j]);
             
             GameObject.Find("Player"+i+playerCharacters[j]).SetActive(false);
         }            
     }
     GameObject.Find("Player1A").SetActive(true);
     GameObject.Find("Player2A").SetActive(true);
 }


menu.jpg (24.4 kB)
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
0

Answer by Firedan1176 · Dec 31, 2015 at 03:38 AM

GameObject.Find only returns active GameObjects, so you can't them when they're disabled! What I would suggest is first, create an empty GameObject to store all your Players (called "Players"). Then, create a List of all of your GameObjects in your script (instead of an array), then simply grab the one you need with an index. I'm not familiar with JavaScript in Unity since I use C#, but I will give you a C# example, which you can then decipher.

 public List<GameObject> players = new List<GameObject>();
 
 int currentlyEnabled = 0;
 
 void Start() {
     GameObject[] foundPlayers = GameObject.Find("Players").GetComponent<GameObject>();
     foreach(GameObject g in foundPlayers) players.add(g);
 }
 
 void PerformAction() {
     //This is where you'd change players.
     players[currentlyEnabled].SetActive(false);    
     currentlyEnabled++;
     players[currentlyEnabled].SetActive(true);
 }

And your hierarchy:

Players

-->A

-->B

Now remember, this is C#, so you'll need to do some changes.

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

44 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 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

UI problems 0 Answers

Navigating multiple menus & scenes 0 Answers

Options menu won't work 0 Answers

How to create scene with multiple panels? 0 Answers

WEIRD EVENT SYSTEM BUTTON SELECTION 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