- Home /
Question by
HuskyPanda213 · Jun 20, 2013 at 04:40 AM ·
gamesavelevelcreator
Level creator save bug.
I have a bug with a level creator save, when I start the game the GetPrefs type script is making like 30 objects and they all are in the same position and are the same color. Please Help.
Level Script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelCreator : MonoBehaviour {
public static LevelCreator ins;
public bool isAddBlock;
public List<block> blocklist = new List<block>();
public GameObject Spawnpoint;
public GameObject Block;
public int blockint;
// Use this for initialization
void Start () {
ins = this;
}
void Update () {
blockint = PlayerPrefs.GetInt("blockint");
}
public float x;
public float y;
public float z;
public string color;
public int isSpawnpoint = 0;
void OnGUI(){
if(GUI.Button(new Rect(Screen.width - 100,5,100,25),"Save")){
SaveBlocks();
}
if(!isAddBlock){
if(GUILayout.Button("Add New Block")){
isAddBlock = true;
}
}
if(isAddBlock){
block temp = new block();
GUILayout.BeginHorizontal();
if(GUILayout.Button("+")){
x++;
}
if(GUILayout.Button("+ 10")){
x+=10;
}
GUILayout.Box("X: " + x);
if(GUILayout.Button("-")){
x--;
}
if(GUILayout.Button("- 10")){
x-=10;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.Button("+")){
y++;
}
if(GUILayout.Button("+ 10")){
y+=10;
}
GUILayout.Box("Y: " + y);
if(GUILayout.Button("-")){
y--;
}
if(GUILayout.Button("- 10")){
y-=10;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.Button("+")){
z++;
}
if(GUILayout.Button("+ 10")){
z+=10;
}
GUILayout.Box("Z: " + z);
if(GUILayout.Button("-")){
z--;
}
if(GUILayout.Button("- 10")){
z-=10;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.Button("Blue")){
color = "blue";
}
if(GUILayout.Button("Green")){
color = "green";
}
if(GUILayout.Button("Grey")){
color = "grey";
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.Button("Is Zombie Spawnpoint")){
isSpawnpoint = 1;
}
if(GUILayout.Button("Is Not Zombie Spawnpoint")){
isSpawnpoint = 0;
}
GUILayout.EndHorizontal();
if(GUILayout.Button("Create Block")){
temp.x = x;
temp.y = y;
temp.z = z;
Addnewblock(color,x,y,z,isSpawnpoint);
}
if(GUILayout.Button("Back")){
isAddBlock = false;
}
GUILayout.Box("Player Spawnpoint at X-0,Y-0,Z-0. Player height is over 2 units, add make sure a ground is at -1 y");
GUILayout.Box("Is zombie spawn: " + temp.isSpawnPoint + " Color = " + temp.color);
}
}
public void Addnewblock(string color,float x,float y,float z,int spawnpoint){
Vector3 pos = new Vector3();
pos.x = x;
pos.y = y;
pos.z = z;
if(color == ""){
color = "grey";
}
block temp = new block();
temp.color = color;
temp.x = x;
temp.z = z;
temp.y = y;
temp.isSpawnPoint = spawnpoint;
blocklist.Add(temp);
blockint++;
PlayerPrefs.SetInt("blockint",blockint);
if(spawnpoint == 0){
if(color == "grey"){
GameObject newblock = Instantiate(Block,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.grey;
}
if(color == "blue"){
GameObject newblock = Instantiate(Block,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.blue;
}
if(color == "green"){
GameObject newblock = Instantiate(Block,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.green;
}
}
if(spawnpoint == 1){
GameObject newspawn = Instantiate(Spawnpoint,pos,Quaternion.identity) as GameObject;
}
}
public void SaveBlocks(){
foreach(block bk in blocklist){
PlayerPrefs.SetString("BlockColor" + blockint,bk.color);
PlayerPrefs.SetInt("BlockSpawn" + blockint,bk.isSpawnPoint);
PlayerPrefs.SetFloat("BlockX" + blockint,bk.x);
PlayerPrefs.SetFloat("BlockY" + blockint,bk.y);
PlayerPrefs.SetFloat("BlockZ" + blockint,bk.z);
}
}
}
[System.Serializable]
public class block{
public string color = "grey";
public int isSpawnPoint = 0;
public float x;
public float y;
public float z;
}
Saveprefs type script.
using UnityEngine;
using System.Collections;
public class SaveCreate : MonoBehaviour {
public int blockint;
public GameObject Blockpre;
public float x;
public float y;
public float z;
public string color;
public int spawnpoint;
// Use this for initialization
void Start () {
blockint = PlayerPrefs.GetInt("blockint");
GetLevel();
}
// Update is called once per frame
void Update () {
}
public void GetLevel(){
for(int counter = 1; counter <= blockint; counter++){
PlayerPrefs.GetString("BlockColor" + blockint);
PlayerPrefs.GetInt("BlockSpawn" + blockint);
PlayerPrefs.GetFloat("BlockX" + blockint);
PlayerPrefs.GetFloat("BlockY" + blockint);
PlayerPrefs.GetFloat("BlockZ" + blockint);
Vector3 pos = new Vector3();
pos.x = x;
pos.y = y;
pos.z = z;
if(color == ""){
color = "grey";
}
if(spawnpoint == 0){
if(color == "grey"){
GameObject newblock = Instantiate(Blockpre,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.grey;
}
if(color == "blue"){
GameObject newblock = Instantiate(Blockpre,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.blue;
}
if(color == "green"){
GameObject newblock = Instantiate(Blockpre,pos,Quaternion.identity) as GameObject;
newblock.renderer.material.color = Color.green;
}
}
//if(spawnpoint == 1){
// GameObject newspawn = Instantiate(Spawnpoint,pos,Quaternion.identity) as GameObject;
//}
}
}
}
Comment
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
preferences, load second time etc 1 Answer
How to save my terrain level? 1 Answer
Problem with acclerometer 0 Answers
Problem with BRDF Shader 1 Answer