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 Wolftic · Feb 16, 2014 at 04:12 PM · nullreferenceexceptionsaveloadsystemnullreference

Getting error on instatiating.

I get a nullreferenceexception error when I try to load my world from a .txt file. I'm using the script from erdroy with some changes: using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.IO; using System.Text;

 public class Save : MonoBehaviour {
     /// <summary>
     /// The objects.
     /// </summary>
     public GameObject[] Objects;
     public string SaveString;
     
     /// <summary>
     /// The objects loaded.
     /// </summary>
     public string LoadString;
     
     /// <summary>
     /// GameObjects
     /// </summary>
     public GameObject[] Objects_;
     
     public string savename = "TEST";
     
     void Start(){
         DontDestroyOnLoad (transform.gameObject);
     }
     
     void Update () {
         if(Input.GetKeyDown(KeyCode.F5))
         {
             SaveGame(savename);
         }
     } 
     
     public void SaveGame(string savename){
         Objects = GameObject.FindGameObjectsWithTag("Savable");
         SaveString = "";
         for(int i = 0; i < Objects.Length; i++)
         {
             SaveString += 
                 Objects[i].name 
                     + ","+ 
                     Objects[i].transform.position.x + "|" + Objects[i].transform.position.y + "|" +Objects[i].transform.position.z + "|"
                     + ","+ 
                     Objects[i].transform.rotation.x + "|" + Objects[i].transform.rotation.y + "|" +Objects[i].transform.rotation.z + "|"
                     + ","+
                     Objects[i].transform.localScale.x + "|" + Objects[i].transform.localScale.y + "|" +Objects[i].transform.localScale.z + "|"
                     + ";";
         }
 
         FileOperations.WriteToFile(savename+".txt", SaveString);
         Debug.Log ("SAVED");
     }
     
     public void LoadGame(string savename){    
         LoadString = FileOperations.ReadFile(savename+".txt");
         string[] ObjectsLoaded = LoadString.Split(';');
         
         foreach(string record in ObjectsLoaded)
         {
             string[] recordSelected = record.Split(',');
             
             string naz, poz1, rot1, scl1;
             string[] poz, rot, scl;
             
             Vector3 position;
             Vector3 scale;
             Quaternion rotation;
             
             naz = recordSelected[0].ToString();
             Debug.Log("Loaded: "+naz);
             
             poz1 = recordSelected[1].ToString();
             Debug.Log("Loaded: "+poz1);
             
             rot1 = recordSelected[2].ToString();
             Debug.Log("Loaded: "+rot1);
 
             scl1 = recordSelected[3].ToString();
             Debug.Log("Loaded: "+scl1);
             
             poz = poz1.Split('|');
             
             rot = rot1.Split('|');
 
             scl = scl1.Split('|');
             
             position.x = Convert.ToSingle(poz[0]);
             position.y = Convert.ToSingle(poz[1]);
             position.z = Convert.ToSingle(poz[2]);
             
             rotation.x = Convert.ToSingle(rot[0]);
             rotation.y = Convert.ToSingle(rot[1]);
             rotation.z = Convert.ToSingle(rot[2]);
             rotation.w = 1;
 
             scale.x = Convert.ToSingle(scl[0]);
             scale.y = Convert.ToSingle(scl[1]);
             scale.z = Convert.ToSingle(scl[2]);
             
             if(naz == Objects_[0].name || naz == Objects_[0].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[0], position, rotation) as Transform;
                 go.localScale = scale;
             }
 
             if(naz == Objects_[1].name || naz == Objects_[1].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[1], position, rotation) as Transform;
                 go.localScale = scale;
             }
 
             if(naz == Objects_[2].name || naz == Objects_[2].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[2], position, rotation) as Transform;
                 go.localScale = scale;
             }
             
             if(naz == Objects_[3].name || naz == Objects_[3].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[3], position, rotation) as Transform;
                 go.localScale = scale;
             }
             
             if(naz == Objects_[4].name || naz == Objects_[4].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[4], position, rotation) as Transform;
                 go.localScale = scale;
             }
             
             if(naz == Objects_[5].name || naz == Objects_[5].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[5], position, rotation) as Transform;
                 go.localScale = scale;
             }
             
             /*if(naz == Objects_[6].name || naz == Objects_[6].name + "(Clone)")
             {
                 Transform go = Instantiate(Objects_[6], position, rotation) as Transform;
                 go.localScale = scale;
             }*/
 
         }
     }
 }
 //By Erdroy (c) HLTC
 //

I'm getting an error at "go.localScale = scale;", saying:

NullReferenceException: Object reference not set to an instance of an object

Save.LoadGame (System.String savename) (at Assets/Scripts/Save.cs:114) PlayerController.Start () (at Assets/Scripts/PlayerController.cs:50) When I remove all of the scale thingy's I get this error:

IndexOutOfRangeException: Array index is out of range.

Save.LoadGame (System.String savename) (at Assets/Scripts/Save.cs:77) PlayerController.Start () (at Assets/Scripts/PlayerController.cs:50) line 77: "poz1 = recordSelected[1].ToString();" Please help me, I need this for my game.
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

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

Scene Saving 1 Answer

simple checkpoint/save/load system in javascript 4 Answers

Problem with Loading States 0 Answers

half life save/load system ? 1 Answer

Null Reference Exception even after check 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