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 /
avatar image
0
Question by Thomas-Hawk · Apr 26, 2017 at 07:00 PM · playerprefsresourcesbuildingstartresources.load

Baffling Resources.Load by playerprefs string problem.

This image shows my code. The expected behavior is as follows: An object, instantiated during runtime, runs a start function which saves its resource as a string. So, if I place a floor, it saves "Floor 2" (the model in the resources that was placed) as a playerprefs string "building0". So this script, loads from player preferences, the same thing based on a list, where the value which provided that "0" is also saved and loaded with player preferences. A while statement is used, then, to look for "Floor 2" in the "Buildings" folder in the resources, which I've structured properly as you can see. I've tried all kinds of casting and syntax all over the instantiate and load lines, but that line just will not load the object, even though I have confirmed that "Buildings/Floor 2" definitely exists, as a prefab.

Here is the text version of the code(Has some redundant stuff, I know):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Thingspace;
 public class buildingSaver : MonoBehaviour {
     public Things.SaveBuilding[] builtObjs;
 
 
     public int buildings;
     public GameObject thething;
     // Use this for initialization
     void Start () {
         buildings = PlayerPrefs.GetInt ("howManyBuildings");
 
         while (buildings > 0) {
             string loadbuildresource = ("Buildings/" + PlayerPrefs.GetString ("building" + buildings.ToString ()));
             Vector3 loadbuildposition = PlayerPrefsX.GetVector3 ("buildingpos" + buildings.ToString());
             Vector3 loadeuler = PlayerPrefsX.GetVector3 ("buildingrot" + buildings.ToString ());
             Debug.Log ("Looking for this in Resource folder: " + loadbuildresource);
             thething = Resources.Load<GameObject>(loadbuildresource);
             Debug.Log("The thing is? "+  thething.name);
             GameObject place = (GameObject)Instantiate (thething, loadbuildposition, Quaternion.Euler (loadeuler));
             buildings--;
         }
     }
 
     
     // Update is called once per frame
     void Update () {
         
     }
 
 
     void SaveBuildings(){
         //Find everything needing saved.
     //    builtObjs = GameObject.FindGameObjectsWithTag
 
     }
 }
 

What is the problem, do you think? alt text

savestructuresproblem.png (92.9 kB)
Comment
Add comment · Show 7
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 Glurth · Apr 26, 2017 at 07:21 PM 0
Share

Shot in the dark here, but make sure you specify ".asset" at the end of the path. If you do not store this in your player pref string; concatenate it after getting the pref, but before calling Resources.Load.

avatar image Thomas-Hawk Glurth · Apr 26, 2017 at 07:44 PM 0
Share

So, you are saying, I would want to be looking for "Buildings/Floor 2.asset"?

avatar image Glurth Thomas-Hawk · Apr 26, 2017 at 07:54 PM 0
Share

Yep. I've hit such an issue previously; not sure if it's your problem here, but worth a shot.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thomas-Hawk · Apr 26, 2017 at 09:23 PM

Ok, I think it was just a mix of user error and order of operations issues, like everything always is.

Here is the final working script. This goes on an empty gameobject, this is called Saver but is actually loading the objects. change the name at your leisure. Requires PlayerPrefsX. using System.Collections; using System.Collections.Generic; using UnityEngine; public class buildingSaver : MonoBehaviour {

     public bool reset;
     public int buildings;
 
     void Start () {
         if (reset) {
             PlayerPrefs.DeleteAll ();
             buildings = 0;
             PlayerPrefs.SetInt ("howManyBuildings", 0);
             //Debug.Break ();
         }
         buildings = PlayerPrefs.GetInt ("howManyBuildings");
         //buildings -= 1;
         while (buildings > 0) {
             Debug.Log (buildings.ToString ());
             string loadbuildresource = (PlayerPrefs.GetString ("building" + buildings.ToString ()));
             Vector3 loadbuildposition = PlayerPrefsX.GetVector3 ("buildingpos" + buildings.ToString());
             Vector3 loadeuler = PlayerPrefsX.GetVector3 ("buildingrot" + buildings.ToString ());
             Debug.Log ("Looking for this in Resource folder: " + loadbuildresource);
             GameObject thething = (Resources.Load <GameObject>(loadbuildresource)as GameObject);
             Debug.Log("The thing is? "+  thething.name);
             Debug.Log(PlayerPrefs.GetString("building"+buildings.ToString()));
             GameObject place = Instantiate (thething, loadbuildposition, Quaternion.Euler (loadeuler));
             Debug.Log ("Instantiated thing , " + place.name);
             buildings--;
         }
     }
 
     
 
 
 }
 


And then just put this script on anything that gets instantiated during runtime and you would like to be there the next time the scene starts. This example finds the other script by looking for a gameobject named BuildingSaver.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SaveObj : MonoBehaviour {

 public buildingSaver saver;
 public string resourceName;
 // Use this for initialization
 void Start () {
     saver = GameObject.Find ("BuildingSaver").GetComponent<buildingSaver> ();
     string thebuilding = ("building" + saver.buildings.ToString ());
     PlayerPrefs.SetString(thebuilding, resourceName);
     string theplace = ("buildingpos" + saver.buildings.ToString ());
     PlayerPrefsX.SetVector3 (theplace, transform.position);
     string therotation = ("buildingrot" + saver.buildings.ToString ());
     //Vector3 rotvector = transform.eulerAngles
     PlayerPrefsX.SetVector3 (therotation, transform.eulerAngles);

     saver.buildings++;
     PlayerPrefs.SetInt ("howManyBuildings", saver.buildings);
     Debug.Log (thebuilding + " " + theplace + " " + therotation + " " + resourceName);
 }

}

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

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

Game lags when loading AudioClip from Resources 1 Answer

Get folder names during runtime within "Resources" package? 1 Answer

Can't restart my game after game over 1 Answer

About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer

What does Unity actually do when Loading Resources that already exist? 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