- Home /
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?
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.
So, you are saying, I would want to be looking for "Buildings/Floor 2.asset"?
Yep. I've hit such an issue previously; not sure if it's your problem here, but worth a shot.
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);
}
}
Your answer
Follow this Question
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