标签: unity-game-engine

脚本问题 - Unity5 C#

码:

if (direction.magnitude > 5f) { 
    this.transform.Translate (0, 0, 0.001f);
    anim.SetBool ("isWalking", true);
}// if 

else
    print ("MAGNITUDE: " + direction.magnitude + " IS GREATER THAN 5");
Run Code Online (Sandbox Code Playgroud)

结果:

MAGNITUDE: 0.786 IS GREATER THAN 5
Run Code Online (Sandbox Code Playgroud)

0.786如何变得超过5

方向变量是Vector3,Vector3.magnitude返回float我真的不知道发生了什么.

谢谢.

c# unity-game-engine

-3
推荐指数
1
解决办法
53
查看次数

尝试运行协同程序时,我不断收到错误

我是C#和编程的新手,我无法让协程工作,我以前使用过基本版并且没有麻烦,我现在正试图做一些非常相似但没有任何成功的东西.

统一的错误信息:

参数#1' cannot convert方法组'表达式输入`System.Collections.IEnumerator'

"UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)"的最佳重载方法匹配有一些无效的参数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Fire : MonoBehaviour
{
public Transform firePos;
public GameObject bullet;
public bool fireCheck;
public float spawnTime;

IEnumerator FireRate()
{
    while(fireCheck == true)
    {
        yield return new WaitForSeconds(spawnTime);
        Instantiate(bullet, firePos.position, firePos.rotation);
    }
}

void Start()
{
    spawnTime = 4f;
    StartCoroutine(FireRate)();
}

void Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        fireCheck = true;
    }

}  
}
Run Code Online (Sandbox Code Playgroud)

我将继续研究并更好地理解这一点,但我无法弄清楚这一点,我将不胜感激

c# unity-game-engine

-3
推荐指数
1
解决办法
396
查看次数

理解为什么我在get方法中遇到递归错误

我目前正在学习属性并遇到了一些问题.当我在get方法中返回我的属性时,我收到一个递归错误.

这是因为每当我返回属性时它会激活get方法,它返回属性,激活get方法等?

这是我的代码:

using UnityEngine;

struct Enemy
{
    public int Bonus;
    private int gold; 

    public int Gold
    {
        get
        {
            return Gold + Bonus;  
        }                       
        set                 
        {
            gold = value; 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# properties unity-game-engine

-3
推荐指数
1
解决办法
48
查看次数

为什么团结一致我收到警告:您正在尝试使用'new'关键字创建MonoBehaviour?

完整的警告信息:

您正在尝试MonoBehaviour使用new关键字创建一个.这是不允许的.MonoBehaviours只能添加使用AddComponent().或者,您的脚本可以继承ScriptableObject或根本不继承基类

在这两个脚本中,我没有使用new关键字:

DetectPlayer脚本:

public class DetectPlayer : MonoBehaviour 
{
    private int counter = 0;

    private void OnGUI()
    {
        GUI.Box(new Rect(300, 300, 200, 20),
            "Times lift moved up and down " + counter);
    }
}
Run Code Online (Sandbox Code Playgroud)

Lift脚本:

public class Lift : MonoBehaviour 
{
    private bool pressedButton = false;
    private DetectPlayer dp = new DetectPlayer();

    private void OnGUI()
    {
        if (pressedButton)
            GUI.Box(new Rect(300, 300, 200, 20), …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

-4
推荐指数
1
解决办法
3863
查看次数

通过脚本获取游戏对象的孩子数量

标题说明了一切:gameObject通过脚本找出有多少孩子的最佳方法是什么。

c# unity-game-engine

-4
推荐指数
1
解决办法
4253
查看次数

如何在unity3d中使用c#获取模型描述列表?

如何为3D角色模型定义模型描述列表(如颈部,脊柱,下唇等)?我试着编写一个脚本:

using UnityEngine;
using System.Collections;
public class Arrays : MonoBehaviour
{
    public GameObject[] players;

    void Start()
    {
        players = GameObject.FindGameObjectsWithTag("avatar_5");  
        for (int i = 0; i < players.Length; i++)
        {
            Debug.Log(players[i].name);
            Debug.Log("Player Number " + i + " is named " + players[i].name);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但结果我收到此错误: UnityException:标签:avatar_5未定义

c# unity-game-engine

-4
推荐指数
1
解决办法
469
查看次数

将纹理添加到未点亮的透明色彩着色器

如何添加TextureOculus API提供的以下着色器以进行淡化:

Shader "Oculus/Unlit Transparent Color" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" {} //  I added this property to apply Texture. Where can I use it?
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100
    Fog {Mode Off}
    Cull Off
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    Color [_Color]

    Pass {}
}
}
Run Code Online (Sandbox Code Playgroud)

shader cg unity-game-engine oculus

-5
推荐指数
1
解决办法
2122
查看次数

C#中的访问变量

我有一个我想解决的问题.我有两节课

public class Class1
{
   [ReadOnly] public int Selector
   private void Start()
   {
      Selector = Random.Range(0, 4);
      Debug.Log("Selectorul " + selector);
   }
}

public class Class2
{
   private Class1 sp;
   private void Start()
   {
       Debug.log(sp.Selector);
   }
}
Run Code Online (Sandbox Code Playgroud)

我是初学者,我想了解为什么当我启动Unity第二个类时,选择器总是0?也许我不对!救命!!!

c# variables unity-game-engine

-5
推荐指数
1
解决办法
117
查看次数

虽然循环在 Unity3D 中冻结游戏

我一直在与 while 循环作斗争,因为它们几乎不适合我。它们总是导致我的 Unity3D 应用程序冻结,但在这种情况下,我真的需要它来工作:

bool gameOver = false;
bool spawned = false;
float timer = 4f;

void Update () 
{
    while (!gameOver)
    {
        if (!spawned)
        {
            //Do something
        }
        else if (timer >= 2.0f)
        {
            //Do something else
        }
        else
        {
            timer += Time.deltaTime;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望这些 if 语句在游戏运行时运行。现在它使程序崩溃,我知道这是 while 循环的问题,因为它在我取消注释的任何时候都会冻结。

c# while-loop unity-game-engine

-5
推荐指数
1
解决办法
3503
查看次数

Assets/Script/Scoreboard.cs(33,2): 错误 CS1525: 意外符号“{”

检查了脚本但仍然没有解决

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scoreboard : MonoBehaviour 
{

    public GameObject[] stars;
    private int coinsCount;
    void Start()
    {
         coinsCount = GameObject.FindGameObjectsWithTag("coin").Length;
    }


    public void starsAcheived()
    {
        int coinsLeft = GameObject.FindGameObjectsWithTag("coin").Length;
        int coinsCollected = coinsCount - coinsLeft;

        float percentage = float.Parse( coinsCollected.ToString()) / float.Parse(coinsCount.ToString()) * 100f;

         if (percentage >= 33f && percentage < 65)
        {
          stars[0].SetActive(true);//one stars
        }
        else if (percentage >= 65 && percentage < 76)
        {
         stars[0].SetActive(true);
         stars[1].SetActive(true);// two stars
        }
        else (percentage>= 76)
        { …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

-5
推荐指数
1
解决办法
68
查看次数

标签 统计

unity-game-engine ×10

c# ×9

cg ×1

oculus ×1

properties ×1

shader ×1

variables ×1

while-loop ×1