标签: unity-game-engine

无法将类型“UnityEngine.GameObject”隐式转换为 ScriptName

游戏管理器

public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    public SlingShot slingshot;


    // Use this for initialization
    void Start()
    {

        int character = PlayerPrefs.GetInt ("CharacterSelected", 0);
            if(character==0)
            {
                slingshot = GameObject.Find ("slingshot");
            }

    }
Run Code Online (Sandbox Code Playgroud)

slingshot 是 SlingShot.cs 的一个实例,上面的代码我试图在用户从菜单中选择时动态设置我的弹弓。我找不到这有什么问题,但它给了我错误Assets/Scripts/GameManager.cs(50,28): error CS0029: Cannot implicitly convert typeUnityEngine.GameObject' toSlingShot'

这个你能帮我吗。谢谢

c# unity-game-engine

-2
推荐指数
1
解决办法
5254
查看次数

协程没有正确地进行

嗨我在c#中的脚本的以下协程部分有问题.我得到的错误信息附在屏幕抓取中,似乎是:GetComponent<Renderer>().

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class test: MonoBehaviour
{
    public float model2;
    public float model1;

    public void section1()
    {
        SceneManager.LoadScene("section1", LoadSceneMode.Single);
    }

    public void fade()
    {
       StartCoroutine(colorlerpin());
    }

    public IEnumerator colorlerpin()
    {    
        float ElapsedTime2 = 0.0f;
        float TotalTime2 = 1f;
        while (ElapsedTime2 < TotalTime2)
        {
            //  fades out main heart
            ElapsedTime2 += Time.deltaTime;
            model1.GetComponent<Renderer>().material.color = Color.Lerp(new Color(1f, 1f, 1f, 1f), new Color(1f, 1f, 1f, 0f), (ElapsedTime2 / TotalTime2));
            yield return null;
            //  fades in cutaway …
Run Code Online (Sandbox Code Playgroud)

c# coroutine unity-game-engine oculus

-2
推荐指数
1
解决办法
43
查看次数

统一动画制作器中的anim.SetBool(“ IsWalking”,walk)错误

在Unity学习部分-生存射击游戏中,我得到一个错误,显示以下错误

类型“ UnityEngine.Animation”不包含“ SetBool”的定义,找不到类型为“ UnityEngine.Animation”的扩展方法“ SetBool”。您是否缺少装配参考?

我检查了文件和组件的名称。

using UnityEngine;

public class PlayerMovement: MonoBehaviour
{
    public float speed = 6f;

    Vector3 movement;
    Animation anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 100f;

    void Awake()
    {
        floorMask = LayerMask.GetMask("Floor");
        anim = GetComponent<Animation>();
        playerRigidbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Verticle");

        Move(h, v);
        Turning();
        Animating(h, v);
    }

    void Move(float h, float v)
    {
        movement.Set(h, 0f, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition(transform.position + …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

-2
推荐指数
1
解决办法
168
查看次数

AddRange引发“无法隐式转换类型”异常

我不知道为什么这不起作用:

    Dictionary<string, List<GameObject>> prefabs = new Dictionary<string, List<GameObject>>();
    List<GameObject> slotPrefabs = new List<GameObject>();

    // yadda yadda yadda

    if (prefabs.ContainsKey(slot))
    {
        prefabs[slot] = prefabs[slot].AddRange(slotPrefabs);
    }
    else
    {
        prefabs.Add(slot, slotPrefabs);
    }
Run Code Online (Sandbox Code Playgroud)

它给了我:

无法将类型“ void”隐式转换为“ System.Collections.Generic.List”

我只想添加到字典键的现有列表(如果已经存在)。

c# unity-game-engine

-2
推荐指数
1
解决办法
42
查看次数

我想在运行时创建一个对象数组

我正在创建一个版本的河内塔拼图。它在 Unity 层次结构中的表示方式是

-游戏对象
-----PegA
---------Arm
---------Base

抱歉,我不知道如何表示层次结构。

场景中有 3 个“钉子”和 7 个“环”作为对象。钉和环在层次结构中处于同一级别。

很明显,我可以“SerializeField”环类,只需在检查器中单击每个 Peg 并将其拖到它们上,但我想要做的只是在运行时将它们添加到代码中。这是我尝试过的。

这是我的戒指课程的一部分

public class ring : MonoBehaviour
{
    public bool locked, resting;
    private float startX, startY, deltaX, deltaY;
    private Vector3 mousePos, beforeDrag;

    private List<GameObject> pegs;

    // Start is called before the first frame update
    void Start()
    {
        startX = transform.position.x;
        startY = transform.position.y;
        locked = false;
        pegs.Add(GameObject.Find("PegA"));
        pegs.Add(GameObject.Find("PegB"));
        pegs.Add(GameObject.Find("PegC"));
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是“对象引用未设置为对象的实例”

有人可以解释一下吗?

c# unity-game-engine

-2
推荐指数
1
解决办法
102
查看次数

在Unity中使长名称更短?

而不是编写像

FindObjectOfType<GameManager>().gameOver()
Run Code Online (Sandbox Code Playgroud)

我只想输入

gm.gameOver()
Run Code Online (Sandbox Code Playgroud)

有没有办法在Unity中做到这一点?

也许使用某种别名,或某种名称空间或其他名称。我将代码清理干净后,因此在寻找使用GameManager的每个文件中使用GameManger gm = FindObjectOfType()并不是我想要的。

alias unity-game-engine

-2
推荐指数
1
解决办法
66
查看次数

2019.1.9如何统一黑白相机?

我想制作一个统一黑白的滤镜相机 2019.1.9 我该如何制作?那可以每次都改成RGB和黑白

c# unity-game-engine

-2
推荐指数
1
解决办法
3770
查看次数

这是我在 YouTube 视频中遵循的代码,但它不起作用

我按照这个YouTube 教程代码一直到 11:55 并完成了所有操作,但我遇到了两个错误:

当前上下文中不存在名称“Move”,并且方法必须具有返回类型。

这是混合树用动画移动角色的代码:

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

public class PlayerController : MonoBehaviour
{
    private Animator _animator;



    // Start is called before the first frame update
    void Start()
    {
        _animator = GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        if (_animator == null) return;

        var x = Input.GetAxis("Horizontal");
        var y = Input.GetAxis("Vertical");

        Move(x, y);
    }
    private Move(float x, float y)
    {
        _animator.SetFloat("VelX", x);
        _animator.SetFloat("VelY", y);
    }

}
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

-2
推荐指数
1
解决办法
82
查看次数

if 语句未在 c# unity 中执行

我试图让一个角色移动一定距离,然后在他们移动一定距离时改变方向......我的代码中的逻辑可能是错误的我仍然需要解决这不是问题,问题是我的 if 语句不执行

public class EnemyControl : MonoBehaviour
{
    private int xMoveDirection=-1;
    private float x;
    void Start()
    {
        x=gameObject.transform.position.x;
    }
    void Update()
    {
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(xMoveDirection,0);
        if(x==0.00){
          Debug.Log("helloimhere");
          xMoveDirection=0;
          x=x+1;
        }
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine game-physics

-2
推荐指数
1
解决办法
50
查看次数

为什么最好使用 Awake 和 OnDestroy 而不是构造函数和析构函数?

我是 Unity 的新手,有我的流程 - 我有一个对象,我需要初始化我的.dll,然后我还需要某个地方来实现我的.dll,所以有 2 个选项(就我而言)来做到这一点

public class Player : MonoBehaviour
{
    Player()
    {
        //1 option for call some INIT methods from .dll
    }

    ~Player()
    {
        //1 option for call some RELEASE methods from .dll
    }

    private void Awake()
    {
        //2 option for call some INIT methods from .dll
    }

    private void OnDestroy()
    {
        //2 option for call some RELEASE methods from .dll
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上,我为此目的使用 constructor + destructor,但根据我遇到的示例,我明白这不是 Unity 中的最佳实践,所以问题是 - 为什么?这里有什么问题?

unity-game-engine

-2
推荐指数
1
解决办法
225
查看次数

标签 统计

unity-game-engine ×10

c# ×8

alias ×1

coroutine ×1

game-physics ×1

oculus ×1