[英]How can i make it so money goes up instead of repeating?
So i'm creating a 2D game in unity using c#.所以我正在使用 c# 统一创建一个 2D 游戏。 Right now when i play the game and i pickup a gem money goes from 0 to 1 and if i pickup another gem it does 0 to 1 again, i'm not sure why it keeps going back to 0. What do i change to make it so when i pickup gems the money does increase and make sure it works with the savesystem.
现在,当我玩游戏时,我捡到一个宝石,钱从 0 变为 1,如果我捡到另一个宝石,它又从 0 变为 1,我不知道为什么它一直回到 0。我要做什么改变因此,当我捡起宝石时,钱确实会增加,并确保它可以与保存系统一起使用。 I have 4 scripts: GemPickup, Player, Playerdata and SaveSystem.
我有 4 个脚本:GemPickup、Player、Playerdata 和 SaveSystem。
Please help as I need to sort this out soon as i have to finish the game by end of November.请帮忙,因为我必须在 11 月底之前完成游戏,所以我需要尽快解决这个问题。
GemPickup Script: GemPickup 脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gempickup : Player
{
public void OnTriggerEnter2D(Collider2D Collision)
{
if (Collision.gameObject.tag.Equals("Player"))
{
Destroy(gameObject);
money = money + 1;
}
}
}
Player Script:播放器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public int level;
public int health;
public int money;
public void Update()
{
if (Input.GetKeyDown("k"))
{
SaveSystem.SavePlayer(this);
}
if (Input.GetKeyDown("l"))
{
PlayerData data = SaveSystem.Loadplayer();
level = data.level;
health = data.health;
money = data.money;
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
}
}
PlayerData script: PlayerData 脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int level;
public int health;
public int money;
public float[] position;
public PlayerData(Player player)
{
level = player.level;
health = player.health;
money = player.money;
position = new float[3];
position[0] = player.transform.position.x;
position[1] = player.transform.position.y;
position[2] = player.transform.position.z;
}
}
SaveSystem script:保存系统脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData Loadplayer()
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in" + path);
return null;
}
}
}
try this in player script instead of attach script to gem gameobject.在播放器脚本中尝试此操作,而不是将脚本附加到 gem 游戏对象。 just add Gem tag to gem.
只需将 Gem 标签添加到 gem。
public void OnTriggerEnter2D(Collider2D Collision)
{
if (Collision.gameObject.tag.Equals("Gem"))
{
Destroy(Collision.gameObject);
money = money + 1;
}
}
Not to leave you hanging, and on a side-not that I don't use Unity, I jst want to show you the following code.不是让你悬而未决,而且不是说我不使用 Unity,我只是想向你展示以下代码。 This is how I would design it without unity.
这就是我在没有统一的情况下设计它的方式。
public class Game
{
public class GameObject
{
internal float[] position;
}
static public void Destroy(GameObject go)
{
// some object destruction
}
public class Gem : GameObject
{
// inherits position
}
public class Character : GameObject
{
// inherits position
internal int health;
}
public class Enemy : Character
{
// inherits position and health
public void OnCollision(GameObject other)
{
if (other is Player)
{
health--;
if (health==0)
{
Destroy(this);
}
}
}
}
[System.Serializable]
public class Player : Character
{
// inherits position and health
// don't expose your internals
// and probably should be properties.. but I don't know if they work with the serializer
internal int level;
internal int money;
public void OnCollision(GameObject other)
{
if (other is Gem)
{
Destroy(other);
money++;
}
else if (other is Enemy)
{
health--;
if (health == 0)
{
//game over;
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.