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 Raizekas · Mar 23, 2014 at 08:21 PM · arrayclassesclass object

"NullReferenceExecption: Object reference not set to an instance of an object" error when using array of own made class objects.

So I want to do an interface in which the user could create his profiles, which would be saved in text file. Profile consists of name and keybinds. Everything seems to work if I use only a single class Profile object, but there should be ability to add more than one profile, so I want to have array of class Profile objects and I get the following error: "NullReferenceException: Object reference not set to an instance of an object Profiles.Update () (at Assets/Profiles.js:138)"

 #pragma strict
 import System.IO;
 
 static var name1 : String = "Enter your name";
 
 static var keybind : KeyCode;
 static var keybind1 : KeyCode = KeyCode.W;
 static var keybind2 : KeyCode = KeyCode.S;
 static var keybind3 : KeyCode = KeyCode.A;
 static var keybind4 : KeyCode = KeyCode.D;
 static var keybind5 : KeyCode = KeyCode.U;
 static var keybind6 : KeyCode = KeyCode.O;
 static var n = 0;            // ammount of profiles
 
 var filePath = "C:/Users/Kompas/Desktop/gg.txt";
 
 class Profile
 {
     private var name : String;
     private var move_up : KeyCode = KeyCode.W;
     private var move_down : KeyCode = KeyCode.S;
     private var move_left : KeyCode = KeyCode.A;
     private var move_right : KeyCode = KeyCode.D;
     private var rotate_left : KeyCode = KeyCode.U;
     private var rotate_right : KeyCode = KeyCode.O;
     
     // Konstruktorius
     function Profile()
     {}
     // Name
     function GetName()
     {
         return name;
     }
     function SetName(name1 : String)
     {
         name = name1;
     }
     // Move up
     function GetMoveUp()
     {
         return move_up;
     }
     function SetMoveUp(move_up1 : KeyCode)
     {
         move_up = move_up1;
     }
     // Move down
     function GetMoveDown()
     {
         return move_down;
     }
     function SetMoveDown(move_down1 : KeyCode)
     {
         move_down = move_down1;
     }
     // Move left
     function GetMoveLeft()
     {
         return move_left;
     }
     function SetMoveLeft(move_left1 : KeyCode)
     {
         move_left = move_left1;
     }
     // Move right
     function GetMoveRight ()
     {
         return move_right;
     }
     function SetMoveRight(move_right1 : KeyCode)
     {
         move_right = move_right1;
     }
     // Rotate left
     function GetRotateLeft()
     {
         return rotate_left;
     }
     function SetRotateLeft(rotate_left1 : KeyCode)
     {
         rotate_left = rotate_left1;
     }
     // Rotate right
     function GetRotateRight()
     {
         return rotate_right;
     }
     function SetRotateRight(rotate_right1 : KeyCode)
     {
         rotate_right = rotate_right1;
     }
 }
 
 
 
     static var profile1 : Profile[] = new Profile[10];
 
 
 function Start () {
 }
 
 function Update () {
     switch(GameManager.change_key)
     {
     case 1:
         keybind1 = keybind;
         break;
     case 2:
         keybind2 = keybind;
         break;
     case 3:
         keybind3 = keybind;
         break;
     case 4:
         keybind4 = keybind;
         break;
     case 5:
         keybind5 = keybind;
         break;
     case 6:
         keybind6 = keybind;
         break;
         
     }
     if (GameManager.save_profile)
     {
         profile1[0].SetMoveUp(keybind1);
         profile1[0].SetName(name1);
         var sw : StreamWriter = new StreamWriter (filePath);
         sw.WriteLine(profile1[0].GetMoveUp() + "" + profile1[1].GetName());
         sw.Flush();
         sw.Close();
         GameManager.save_profile = false;
         n++;
     //    if (profile1.move_up != "None");
     //    GameManager.change_key = 0;
     }
 }
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
Best Answer

Answer by robertbu · Mar 23, 2014 at 08:44 PM

You create the array, but that array is just an array of null pointers. You also have to create an instance of the class Profile for any entry in the array you want to use. Given your structure, I'm not sure where the best place to insert it is. You could insert between line 127 and 128:

 if (profile1[0] == null)
      provile1[0] = new Profile();
Comment
Add comment · Show 1 · 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 Raizekas · Mar 23, 2014 at 08:58 PM 0
Share

Damm, I thought that solution will be something simple, but didn't even imagine it would be so simple. Huge thanks, pal.

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

20 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

Related Questions

A list in a class... Am I doing it wrong?? 0 Answers

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

Make an array containting multiple classes 1 Answer

If use Contructor in my program Contructor is calling two times,If I use Constructor in my Program Constructor is calling 4 times why? 0 Answers

Access a variable of a class stored in an array. 2 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