In this Game picture, you can see “Score : 22”. when start ‘play’, change into “Score : 0”
When I push the ‘s’ on keyboard, the Score must add ‘+5’ but still Zero.
Here is the script about it. ( 3 each )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public static UIManager Instance;
public Text scoreText;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
public void UpdateScore (int score)
{
scoreText.text = "Score : " + score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public float scrollSpeed;
public float length;
public Transform backTransform;
private bool moveOn;
// Use this for initialization
void Start ()
{
StartCoroutine (MoveBackground ());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator MoveBackground()
{
Vector3 startPosition = backTransform.position;
moveOn = true;
while (moveOn)
{
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, length);
backTransform.position = startPosition + Vector3.left * newPosition;
yield return null;
}
}
public void AddScore(int amount)
{
AllyShip.Instance.thisShipScore = AllyShip.Instance.thisShipScore + amount;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AllyShip : Ship, IAlly
{
public static AllyShip Instance;
public int defenseRate;
public MissileWeapon missileWeapon;
public BulletWeapon bulletWeapon;
public List<WeaponClass> weapons = new List<WeaponClass> ();
public WeaponClass thisWeaponClass;
public Weapon thisWeaponSort;
public Stat reserveStat;
public GameObject damPrefab;
public GameObject hpBar;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start ()
{
weapons.Add(missileWeapon);
weapons.Add(bulletWeapon);
thisWeapon = Weapon.Missile;
thisShipScore = 0;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown ("a"))
{
DealDamage (3);
}
if(Input.GetKeyDown("s"))
{
GameManager.Instance.AddScore (5); // this is not working as [B]AllyShip.cs:43[/B] why ??? Help...
}
}
public override void DealDamage(int damage)
{
float finalDamage = damage - damage * (defenseRate / 100f);
HP -= (int)finalDamage;
GameObject damObj = (GameObject)Instantiate (damPrefab, transform.position, Quaternion.identity);
damObj.GetComponentInChildren<Text> ().text = finalDamage.ToString ();
damObj.transform.parent = transform;
damObj.transform.localEulerAngles = new Vector3 (0, -90f, 0);
Destroy (damObj, 3);
SetHPBar ((float)HP / (float)MaxHP);
}
public void SetHPBar(float hpValue)
{
hpBar.transform.localScale = new Vector3 (hpValue, hpBar.transform.localScale.y, hpBar.transform.localScale.z);
}
public override void OnConflict(GameObject other)
{
if (other.GetComponent<EnemyShip>() != null)
{
HP -= other.GetComponent<EnemyShip>().collideDamage;
}
}
public Weapon thisWeapon
{
get
{
return thisWeaponSort;
}
set
{
thisWeaponSort = value;
foreach (WeaponClass weapon in weapons)
{
if (weapon.weaponSort == value)
thisWeaponClass = weapon;
}
}
}
private int Score;
public int thisShipScore
{
get
{
return Score;
}
set
{
Score = value;
// UIManager for update the score
UIManager.Instance.UpdateScore(Score);
}
}
public void AllyMethod()
{
Debug.Log (Name + "AllyMethod is Called");
}
void OnCollisionEnter(Collision other)
{
OnConflict (other.gameObject);
Debug.Log ("This ship conflict with " + other.gameObject.name);
}
}
“Object reference not set to an instance of an object”
This message have been seeing.
I just typed the examples by studying.
But that error message is now shown.
Please help me.