- Home /
A real head-scratcher
I am trying to get health of a monster at the start, and it works absolutly fine, but when I see in the inspector I see "0" health. in the start it works fine, it shows the right health "200", but a second later it shows in inspector of the monster + in the console that the health is "0". so at the start the monster script should get the health (int) from another script. l hope you can find out what's wrong, because trust me I am good at coding but im trying to figure out over 2 days what's wrong. Thanks a lot for even reading this! I've tried EVERYTHING I CAN, I get the health from the database correctly, but it wont work, after the start the "StartingHealth" just resets to "0" IDK why :( please help, thanks! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MonsterData : MonoBehaviour
{
public int MonHealth;
public string[] items;
private string MonsterDataLink = "https://classeskingdom.000webhostapp.com/SetMonster.php";
private string MonsterGetDataLink = "https://classeskingdom.000webhostapp.com/GetMonsterData.php";
[SerializeField] private string MonsterName;
[SerializeField] private int Health,Exp, RunesMin, RunesMax, Item,Item1,Item2,Item3,Item4,Item5,Item6,Item7,Level,DEF,Attack;
[SerializeField] private int MonsterID;
private void Start()
{
//StartCoroutine(GetMonsterData(MonsterID));
//StartCoroutine(CreateMonster(MonsterName,Health, Exp, RunesMin,RunesMax, Item, Item1, Item2, Item3, Item4, Item5, Item6, Item7,Level,DEF,Attack));
}
IEnumerator CreateMonster(string MonsterName,int Health,int Exp,int RunesMin,int RunesMax,int Item,int Item1,int Item2,int Item3,int Item4,int Item5,int Item6,int Item7,int Level,int DEF, int Attack)
{
WWWForm MonsterRegister = new WWWForm();
MonsterRegister.AddField("NamePost", MonsterName);
MonsterRegister.AddField("HealthPost", Health);
MonsterRegister.AddField("ExpPost", Exp);
MonsterRegister.AddField("RunesMinPost", RunesMin);
MonsterRegister.AddField("RunesMaxPost", RunesMax);
MonsterRegister.AddField("ItemPost", Item);
MonsterRegister.AddField("Item1Post", Item1);
MonsterRegister.AddField("Item2Post", Item2);
MonsterRegister.AddField("Item3Post", Item3);
MonsterRegister.AddField("Item4Post", Item4);
MonsterRegister.AddField("Item5Post", Item5);
MonsterRegister.AddField("Item6Post", Item6);
MonsterRegister.AddField("Item7Post", Item7);
MonsterRegister.AddField("LevelPost", Level);
MonsterRegister.AddField("DEFPost", DEF);
MonsterRegister.AddField("AttackPost", Attack);
WWW wwwRegister = new WWW(MonsterDataLink, MonsterRegister);
yield return wwwRegister;
Debug.Log(wwwRegister.text);
}
public IEnumerator GetMonsterData(int MonsterID)
{
WWWForm MonsterGetData = new WWWForm();
MonsterGetData.AddField("MonsterIDPost", MonsterID);
WWW wwwMonsterData = new WWW(MonsterGetDataLink, MonsterGetData);
yield return wwwMonsterData;
string MonsterDataString = wwwMonsterData.text;
items = MonsterDataString.Split(';');
string Data = (GetDataValue(items[MonsterID - 1], "Health:"));
yield return Data;
Debug.Log(Data);
GetInfo(Data);
}
public string GetDataValue(string data, string index)
{
string value = data.Substring(data.IndexOf(index) + index.Length);
value = value.Remove(value.IndexOf("|"));
return value;
}
private void GetInfo(string health)
{
int sethealth = int.Parse(health);
MonsterMove mons = new MonsterMove();
Debug.Log(sethealth);
mons.SetHealth(sethealth);
}
}
//MONSTER CODE: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class MonsterMove : MonoBehaviour
{
#region //start
public int StartingHealth;
private int starts;
[SerializeField] private int MonsterID=1;
public Animator anim;
public bool IdleAnimBool = false;
public bool PatrolAnimBool = true;
[SerializeField]
private float TimeCounterStartPatrol,FixedPatrolTime;
private float RandomTimePatrol, TimeCounterBasePatrol;
[SerializeField]
private float TimeCounterStartIdle,FixedIdleTime;
private float RandomIdleTime, TimeCounterBaseIdle;
[SerializeField]
private bool ShouldBeIdle=false;
public float MonsterMoveSpeed;
public float Distance=2f;
[SerializeField]
private bool ShouldBePatroling = true;
private bool MovingRight = true;
public Transform groundDetection;
#endregion
private void Start()
{
MonsterData mons = new MonsterData();
StartCoroutine(mons.GetMonsterData(MonsterID));
MonsterMoveSpeed = Random.Range(1, 1.3f);
TimeCounterBasePatrol = TimeCounterStartPatrol;
TimeCounterBaseIdle = TimeCounterStartIdle;
RandomTimePatrol = Random.Range(0f, TimeCounterStartPatrol-FixedPatrolTime);
RandomIdleTime = Random.Range(0f, TimeCounterStartIdle-FixedIdleTime);
}
void FixedUpdate()
{
Debug.Log(StartingHealth);
if (ShouldBePatroling&&!ShouldBeIdle)
{
Move();
PatrolTime();
PatrolAnim();
anim.SetBool("run", true);
anim.SetBool("idle", false);
}
if (ShouldBeIdle==true&&!ShouldBePatroling) {
IdleAnim();
IdleTime();
anim.SetBool("run", false);
anim.SetBool("idle", true);
}
}
void Move()
{
transform.Translate(Vector2.right * MonsterMoveSpeed * Time.deltaTime);
int layer_mask1 = LayerMask.GetMask("Ground");
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, Distance, layer_mask1);
if (groundInfo.collider == false)
{
if (MovingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
MovingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
MovingRight = true;
}
}
}
private void PatrolTime()
{
TimeCounterStartPatrol -= Time.fixedDeltaTime;
if (TimeCounterStartPatrol <= RandomTimePatrol)
{
ShouldBePatroling = !ShouldBePatroling;
ShouldBeIdle = !ShouldBeIdle;
TimeCounterStartPatrol = TimeCounterBasePatrol;
}
}
private void IdleTime()
{
TimeCounterStartIdle -= Time.fixedDeltaTime;
if (TimeCounterStartIdle <= RandomIdleTime)
{
ShouldBePatroling = !ShouldBePatroling;
ShouldBeIdle = !ShouldBeIdle;
TimeCounterStartIdle = TimeCounterBaseIdle;
}
}
private void IdleAnim()
{
if (IdleAnimBool == true && PatrolAnimBool == false)
{
IdleAnimBool = false;
PatrolAnimBool = true;
}
}
private void PatrolAnim()
{
if (PatrolAnimBool == true && IdleAnimBool == false)
{
PatrolAnimBool = false;
IdleAnimBool = true;
}
}
private void ChangeAnim()
{
}
public void SetHealth(int health)
{
StartingHealth = health;
}
}