简体   繁体   English

我的 C# 脚本在 Unity 中没有执行任何操作

[英]My C# script isn't doing anything in Unity

I have just started game development in unity.我刚刚开始统一开发游戏。 In the tutorial that I'm following, it tells me to attach my script to my ball (player), which is supposed to make it move.在我正在遵循的教程中,它告诉我将脚本附加到我的球(球员)上,这应该让它移动。 When I run the program though, nothing happens, and when I make it write in console to see if it runs, it doesn't write anything, which makes me think that either something is wrong with my code, or that VS Code doesn't work with unity.但是,当我运行程序时,什么也没有发生,当我让它在控制台中写入以查看它是否运行时,它什么也没写,这让我认为我的代码有问题,或者 VS Code 没有团结一致。 Here's my code in case you want it.这是我的代码,以备不时之需。

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

public class AddConstantVelocity : MonoBehaviour
{
    void Update()
    {
        Console.Writeline("Moving");
        GetComponent<Rigidbody>().velocity = new Vector3(1, 0, 0);
    }
}
  1. If you want to see log in Unity console you should use Debug.Log("smth");如果你想在 Unity 控制台中查看日志,你应该使用Debug.Log("smth");

  2. You don't need getting access to the Rigidbody component in Update (many times per frame), cache it once in Start()您不需要在 Update 中访问 Rigidbody 组件(每帧多次),在Start()中缓存一次

  3. Make sure that you added your script to the GameObject you wanted to move (check Unity debug logs)确保将脚本添加到要移动的游戏对象(检查 Unity 调试日志)

     private Rigidbody rb; private void Start() { rb = GetComponent<Rigidbody>(); } private void Update() { Debug.Log("Moving"); rb.velocity = new Vector3(1, 0, 0); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM