c# - Unity 代码在编辑器中有效,但在 Build C# 中无效
问题描述
首先,我知道还有其他人有同样的问题,不,他们的解决方案对我没有帮助。
我正在为大学做一个期末项目,他们强迫我们使用 Perforce Helix Client(顺便说一句,这简直就是地狱)进行版本控制。
我和我的团队日夜工作,修复编辑器中的问题。然后,我们终于到了可以测试构建的部分。我们的构建工作正常。(是的,我们在 perforce 中删除了旧的构建文件并获得了当前的构建文件)
然后,灾难发生了,当我们的教授测试我们的游戏时,我们的动作脚本不起作用,他给了我们一次机会,但说我们的成绩不能高于这个作业的 70 分。
无论如何,我不是制作这个脚本的人,但我想知道这里是否有任何东西会导致脚本无法在构建中工作。
对于那些不关心背景故事的人-
问题:玩家不会在我们的构建和控制台垃圾邮件 nullreference 中移动。
预期结果:角色四处移动。
我们尝试了什么:删除过去的构建并重新下载当前构建并检查检查器中的所有内容
运动脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
private Vector3 moveInput;
private Vector3 moveVelocity;
private Vector3 mousePosition;
private Camera mainCamera;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
mainCamera = FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(transform.position, mousePosition);
if(distance > 2)
{
moveInput = new Vector3(Input.GetAxisRaw("Horizontal") * 40 * Time.smoothDeltaTime, 0f, Input.GetAxisRaw("Vertical") * 40 * Time.smoothDeltaTime);
moveVelocity = moveInput * moveSpeed * Time.smoothDeltaTime;
}
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 2000))
{
transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
}
}
void FixedUpdate()
{
rb.velocity = moveVelocity;
}
}
积分守门员:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PointsKeeper : MonoBehaviour
{
public int points = 0;
public Text pointText;
public GameObject toExpensiveText;
public float expensiveTimer = 1.0f;
public GameObject healthPage;
public GameObject weaponPage;
public GameObject modPage;
public GameObject lifeBlocker;
public GameObject lifeSector;
public GameObject lifeDropChanceSector;
public GameObject shieldBlocker;
public GameObject shieldRegensector;
public GameObject healthBlocker;
public GameObject healthcapacitysector;
private PlayerHealth playerHealth;
public Sprite fullCell;
public Sprite emptyCell;
//Health Page
private int healprice = 100;
public Text healpriceText;
public GameObject healButton;
public int maxHealthLVL = 0;
private int maxHealthMAXLVL = 5;
private int maxHealthprice = 100;
public Text maxhealthpricetext;
public GameObject maxhealthButton;
public Image[] maxhealthCells;
public int healDropChanceLVL = 0;
private int healdropchanceprice = 100;
public int shieldcapacityLVL = 0;
private int sheildcapacityPrice = 100;
public int shieldRegenRateLVL = 0;
public int lifeDropChanceLVL = 0;
public int lifeCapacityLVL = 0;
private int lifePrice;
//Weapon Page
//Mod Page
// Start is called before the first frame update
void Start()
{
points = 0;
playerHealth = FindObjectOfType<PlayerHealth>();
toExpensiveText.SetActive(false);
}
// Update is called once per frame
void Update()
{
pointText.text = points.ToString();
healpriceText.text = healprice.ToString();
maxhealthpricetext.text = maxHealthprice.ToString();
maxHealthCellControl();
if (lifeCapacityLVL >= 1)
{
lifeBlocker.SetActive(false);
lifeSector.SetActive(true);
lifeDropChanceSector.SetActive(true);
}
if (shieldcapacityLVL >= 1)
{
shieldBlocker.SetActive(false);
shieldRegensector.SetActive(true);
}
if (playerHealth.playerHP == playerHealth.maxheart)
{
healthBlocker.SetActive(false);
healthcapacitysector.SetActive(true);
}
else
{
healthBlocker.SetActive(true);
healthcapacitysector.SetActive(false);
}
if(playerHealth.playerHP == playerHealth.maxheart)
{
healButton.SetActive(false);
}
else
{
healButton.SetActive(true);
}
if (Input.GetKeyDown(KeyCode.P))
{
points = points + 100000;
}
}
public void pointsGained(int pointsup)
{
points = points + pointsup;
}
public void healthPageOn()
{
healthPage.SetActive(true);
modPage.SetActive(false);
weaponPage.SetActive(false);
}
public void weaponPageOn()
{
healthPage.SetActive(false);
modPage.SetActive(false);
weaponPage.SetActive(true);
}
public void modPageOn()
{
healthPage.SetActive(false);
modPage.SetActive(true);
weaponPage.SetActive(false);
}
public void toExpensive()
{
}
public void buyHeal()
{
if(points >= healprice)
{
points = points - healprice;
healprice = healprice + 100;
playerHealth.playerHP = playerHealth.playerHP + 1;
}
else
{
toExpensive();
}
}
public void buyMaxHealth()
{
if (points >= maxHealthprice && maxHealthLVL < 5)
{
maxHealthLVL = maxHealthLVL + 1;
points = points - maxHealthprice;
playerHealth.maxheart = playerHealth.maxheart + 1;
}
else
{
toExpensive();
}
if (maxHealthLVL == 1)
{
maxHealthprice = 1000;
}
else if (maxHealthLVL == 2)
{
maxHealthprice = 2000;
}
else if (maxHealthLVL == 3)
{
maxHealthprice = 4000;
}
else if (maxHealthLVL == 4)
{
maxHealthprice = 10000;
}
else if (maxHealthLVL == 5)
{
maxhealthButton.SetActive(false);
maxhealthpricetext.gameObject.SetActive(false);
}
}
public void maxHealthCellControl()
{
for (int i = 0; i < maxhealthCells.Length; i++)
{
if (i < maxHealthLVL)
{
maxhealthCells[i].sprite = fullCell;
}
else
{
maxhealthCells[i].sprite = emptyCell;
}
if (i < maxHealthMAXLVL)
{
maxhealthCells[i].enabled = true;
}
else
{
maxhealthCells[i].enabled = false;
}
}
}
}
另外,关于如何修复空引用的任何想法都会很好。
抱歉,如果问题不够具体或者我没有提供足够的信息。我仍然感到震惊,并且不经常使用该网站。
解决方案
试试这个,以摆脱异常:
public void maxHealthCellControl()
{
if (maxhealthCells == null) return;
for (int i = 0; i < maxhealthCells.Length; i++)
{
if (i < maxHealthLVL)
{
maxhealthCells[i].sprite = fullCell;
}
else
{
推荐阅读
- c# - 获取阿拉伯月份的英文名称
- git - 如何将 git repo 转换为 word 文档?
- java - 将三个搜索算法存储在一个列表中(作为方法调用)并在 main 中迭代它们?
- jquery - JQuery 不响应 POST 请求
- oracle - 在 PL/SQL 中将二进制转换为 varchar
- html - CSS 转换为 div 提供轮廓
- x86-64 - 64 位机器中虚拟地址的高 16 位设置为零。为什么?
- javascript - Height not increasing using setInterval (creating border around screen)
- java - 解释模拟匹配(str)与相同(str)的这种行为的原因
- angular - Angular6从HTML调用组件