这里有轻微的c#问题.我目前正在使用Unity编写一个小型平台游戏,我有一些用于检查碰撞的光线投射等.
现在,我开始通过将那些光线投射的结果存储到整数数组中来清理代码,但是我得到了一个IndexOutOfRangeException.
我试图多次阅读我的代码,但似乎无法找到导致问题的原因.如果有人可以帮助我,我会很高兴.
这是我的代码:
using UnityEngine;
using System.Collections;
public class PlayerRayCaster : MonoBehaviour {
public float playerHeight = 1;
public enum FeetState {Air, Ground};
public FeetState playerFeetState = FeetState.Air;
public int feetHitRays;
public int behindHitRay;
//Arrays of rays. value of 1 means that ray hits a target, 0 means that it does not hit.
public int[] sideRays; // [0-3] = Left side. [4-8] = Right side.
public int[] depthRays; // [0] = Away from camera. [1] = Towards camera.
public …Run Code Online (Sandbox Code Playgroud) 我刚刚看到Unity3D中的一个警告说使用了
this.gameObject.active = BackdropVisible;
已经过时了.
我昨天只写了它,但显然现在是旧闻;)
但是,设定
this.gameObject.SetActive(true);
工作良好; 但
this.GameObject.SetActive(true);
没有.差异是大写"G" - 在错误消息中使用
任何人都可以清理混乱吗?(我昨天刚开始使用c#)我猜测小写版本是一个变量,文档中的那个是一个对象; 但对于未经训练的眼睛,他们是同一件事.显然他们不是.
我需要从我的脚本中结束统一.好像我点击了GUI中的停止按钮.我可以直接从代码执行此操作吗?我知道,我如何关闭一个场景,但需要关闭所有进程.
这是我的代码.我有对应设置为20,我得到警告可能错误空语句与这些行.我的目标是提高速度,让你越努力.当我输入数字20时会发生同样的问题.到目前为止,它每次只增加0.75.
if(speed < counterpart);
{
speed += 0.25F;
}
if(speed > counterpart);
{
speed += 0.5F;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一款需要得分的游戏.我想将我的变量传递给c#中的另一个场景.我尝试了很多解决方案,但没有一个有效!我有一个公共静态变量作为我的分数,我想将它传递给丢失的屏幕.
因为我已经下载了Unity 5.我遇到了无数的错误.所有都是相似的,所以我用Google搜索它.我找到了一个共同的答案,但我不知道它们的意思.
错误:
Assets/6by7/ProGrids/Scripts/Editor/ProGrids_Base.js(5,42):BCE0144:'UnityEngine.Resources.LoadAssetAtPath(String,System.Type)'已过时.改为使用AssetDatabase.LoadAssetAtPath(UnityUpgradable)
Assets/6by7/ProGrids/Scripts/Editor/ProGrids_Base.js(6,40):BCE0144:'UnityEngine.Resources.LoadAssetAtPath(String,System.Type)'已过时.改为使用AssetDatabase.LoadAssetAtPath(UnityUpgradable)
Assets/6by7/ProGrids/Scripts/Editor/ProGrids_Base.js(7,41):BCE0144:'UnityEngine.Resources.LoadAssetAtPath(String,System.Type)'已过时.改为使用AssetDatabase.LoadAssetAtPath(UnityUpgradable)
Assets/6by7/ProGrids/Scripts/Editor/ProGrids_Base.js(8,46):BCE0144:'UnityEngine.Resources.LoadAssetAtPath(String,System.Type)'已过时.改为使用AssetDatabase.LoadAssetAtPath(UnityUpgradable)
这是常见的答案:
它基本上告诉你该做什么,而不是blahblah.renderer你现在需要做的事情blahblah.GetComponent(Renderer).它看起来更有用,但它现在对所有组件更加通用,更明显的是代码在后台执行的操作.
我不知道它告诉我做什么.
所以看:我在游戏中有一个怪物,当玩家与他发生碰撞时,会出现一个面板并说再试一次.现在有一个盾牌对象,可以保护玩家免于死亡,所以当玩家与它发生碰撞然后如果你触摸怪物你不应该死一次:就像那样,你触摸盾牌和你免受一人死亡.你能帮我写这段代码吗?
public GameObject panel;
public bool hasShield = false; /* no shield in the beginning */
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Shield")
{
hasShield = true; //We are safe now.
/* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
}
else if (col.gameObject.tag == "Monster" && !hasShield)
{
//We hit a monster and had no shield. Display gameover.
panel.SetActive (true);
}
//I assume the panel is inactive by default …Run Code Online (Sandbox Code Playgroud) 我正在制作一个 2d 游戏,我需要淡入和淡出一些元素。
IEnumerator FadeOut(Renderer rend, float speed)
{
while (rend.material.color.a > 0)
{
Color objectColor = rend.material.color;
float fadeAmount = objectColor.a - (speed * Time.deltaTime);
objectColor = new Color(objectColor.r, objectColor.g, objectColor.b, fadeAmount);
rend.material.color = objectColor;
yield return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 FadeOut 协程(FadeIn脚本正是这个协程,但相反)。问题是它会出现错误。例如,如果我多次进入和退出需要淡出的区域,则整个游戏只会混合黑色(或太亮)。我通过使用StopAllCoroutines函数解决了这个问题。协程调用之一:
StopAllCoroutines();
StartCoroutine(FadeOut(tilemap,1f));
Run Code Online (Sandbox Code Playgroud)
我宁愿不使用 StopAllCoroutines,因为我在同一游戏中使用多个协程,进入淡入淡出区域只会关闭一半的系统。
我尝试使用StopCoroutine("name")(但据我所知,它与较新版本的 unity 不兼容)。我还在 StackOverflow 上找到了另一个帖子
来自线程的代码:
routines[col] = StartCoroutine((DamageEverySecond(health, damageRate)));
[...]
StopCoroutine(routine);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并不能解决我的问题,因为有多个实例同时运行。
我怎么解决这个问题?
所以我正在统一制作一个小游戏,你必须射击敌人。然而,当我为子弹和敌人制作脚本时,它一半有效,一半无效。有时,子弹会击中敌人并消灭敌人,但有时,需要多次射击才能发挥作用。但是当我把子弹的速度调到1(子弹的速度是500)时,子弹总是消灭敌人。所以这让我认为这与子弹的速度有关。这是我的脚本
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
Destroy(gameObject);
Debug.Log("e");
}
Run Code Online (Sandbox Code Playgroud)
对于子弹的移动,我只使用了transform.Translate(Vector3.up * Time.deltaTime * speed)。我怎样才能解决这个问题?
我正在关注有关在 Unity 中生成 Minecraft 风格地形的 YouTube 教程,在教程的第 10 部分中,编写了一个脚本,其中包含一个以语句结尾的函数return x switch。switch的具体内容如下:
public static Vector3Int GetVector(this Direction direction)
{
return direction switch
{
Direction.up => Vector3Int.up,
Direction.down => Vector3Int.down,
Direction.right => Vector3Int.right,
Direction.left => Vector3Int.left,
Direction.forward => Vector3Int.forward,
Direction.backwards => Vector3Int.back,
=> throw new Exception("Invalid direction provided.")
};
}
Run Code Online (Sandbox Code Playgroud)
Direction是一个单独脚本中的枚举,其中包含forward, right, backwards, left, up, 和down, 按确切的顺序。问题是,一旦编译了代码,我就会在 Unity 中从 DirectionExtensions.cs(包含返回开关的脚本)收到此错误:
Assets/Scripts/Terrain/DirectionExtensions.cs(17,52):错误 CS8504:模式丢失
我尝试在 Google 上搜索错误代码和错误消息,但结果要么是参考资料(例如 C# 错误消息列表),要么是不相关的 Unity 错误。我没有发现任何实际发生过此错误的例子。不,该错误不是预期的,因此本教程不会演示如何在任何时候修复它。如何解决此错误以便我可以继续处理我的项目?