[英]Changing data on click button
我在某些GameObjects中具有一些功能。 在游戲中購買升級時,需要更改這些功能。 問題在於每個功能都設置在其自己的對象上。
第一個問題是,當我單擊按鈕時,變量不會更改。 如您所見,我已經向按鈕添加了onclick值,說明單擊按鈕時。 該值應更改。
這里的問題是“對象引用未設置為實例”
我面臨的第二個問題是每個彈丸都是獨立發射的。 因此,如果我將靜態傷害更改為1,它將不會轉移到其他彈丸上。
升級菜單
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UpgradeMenu : MonoBehaviour
{
[SerializeField]
private Text accuracyText;
[SerializeField]
private Text speedText;
[SerializeField]
private Text damageText;
[SerializeField]
private float accuracyMultiplier = 0.7f;
private Weapon weapon;
private Projectile projectile;
private Player player;
void OnEnable()
{
UpdateValues();
}
void UpdateValues ()
{
accuracyText.text = weapon.randomAngle.ToString();
damageText.text = projectile.DamageOnHit.ToString();
speedText.text = player.MaxRun.ToString();
}
public void UpgradeAccuracy ()
{
weapon.randomAngle = (int)weapon.randomAngle * accuracyMultiplier;
UpdateValues();
}
public void UpgradeDamage ()
{
projectile.DamageOnHit = (int)projectile.DamageOnHit + 1;
UpdateValues();
}
}
彈丸(DamageScript)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//[RequireComponent (typeof(Rigidbody2D))]
public class Projectile : MonoBehaviour {
[Header ("Speed")]
public float baseSpeed;
public float randomSpeed;
public Vector2 SpeedV2;
public Vector2 Direction;
[Header ("Damage")]
public int DamageOnHit;
[Header ("Layers")]
public LayerMask solid_layer;
public LayerMask entities_layer;
[Header ("OnHit FX")]
public GameObject HitFxPrefab;
public GameObject DustFxPrefab;
[Header ("Bounce")]
public bool BounceOnCollide = false;
public int bouncesLeft = 0;
[HideInInspector]
public Health owner; // owner of the projectile
private Vector2 Position; // Current position
private Vector2 movementCounter = Vector2.zero; // Counter for subpixel movement
public BoxCollider2D myCollider;
List<Health> healthsDamaged = new List<Health>(); // List to store healths damaged
void OnCollideWith (Collider2D col, bool horizontalCol = true) {
var component = col.GetComponent<Health> ();
// If the target the hitbox collided with has a health component and it is not our owner and it is not on the already on the list of healths damaged by the current hitbox
if (component != null && component != owner && !healthsDamaged.Contains(component)) {
// Add the health component to the list of damaged healths
healthsDamaged.Add (component);
// Apply the damage
var didDamage = component.TakeDamage (DamageOnHit);
// Destroy the projectile after applying damage
if (didDamage) {
DestroyMe ();
return;
}
}
// if the projectile hit's a solid object, destroy it
if (col.gameObject.layer == (int)Mathf.Log(solid_layer.value, 2)) {
DestroyMeWall ();
return;
}
}
void OnCollideWithEntity(Collider2D col) {
var component = col.GetComponent<Health> ();
// If the target the hitbox collided with has a health component and it is not our owner and it is not on the already on the list of healths damaged by the current hitbox
if (component != null && component != owner && !healthsDamaged.Contains(component)) {
// Add the health component to the list of damaged healths
healthsDamaged.Add (component);
// Apply the damage
var didDamage = component.TakeDamage (DamageOnHit);
// Destroy the projectile after applying damage
if (didDamage) {
DestroyMe ();
}
}
}
首先,改變
[Header ("Damage")]
public int DamageOnHit;
靜態
public static int DamageOnHit = /*your starting value*/;
這樣可以確保所有彈丸在受到打擊時都受到相同的傷害。
例如,如果您當前在一個場景中有10個彈丸,而DamageOnHit
為2,則它們都會造成2點傷害。
沒有static
,每個射彈將具有自己的DamageOnHit
。 這也將我們帶入下一個情況:
如果每個彈丸都有其自己的DamageOnHit
,並且我們要修改DamageOnHit
,則需要指定要修改的彈丸的傷害。
但是,如果它是靜態的,它將變得更加簡單,因為所有彈丸都共享相同的DamageOnHit
。
現在,如果您想更改所有彈丸的DamageOnHit
,只需執行
Projectile.DamageOnHit = /*Your new damage value*/
另外,由於您從未在UpgradeMenu
分配projectile
,因此發生了null reference exception
。
(請注意,您在UpgradeMenu.cs
從未使用過projectile = /*your projectile*/
嗎?)
默認情況下,這將使變量為null。 並嘗試執行null.DamageOnHit += 1
毫無意義。
小型修改:將變量設為靜態也將意味着您無法將其公開給檢查器。 但是您可以分配一個初始值,如最初顯示的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.