【问题标题】:Only assignment call increment decrement await and new object expressions can be used as a statement Unity Pong Game [duplicate]只有赋值调用递增递减等待和新对象表达式可以用作语句 Unity Pong Game [重复]
【发布时间】:2021-07-03 13:23:49
【问题描述】:

我最近从本教程 https://youtu.be/YHSanceczXY 开始在 Unity 中制作 Pong 游戏,但其中一个代码不起作用

enter code here 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class Goal : MonoBehaviour
    {
    public bool isPlayer1Goal;

    private void OnTriggerEnter2D(Collider collision)
    {
        if (collision.gameObject.CompareTag("Ball"))
        {
            if (!isPlayer1Goal)
            {
                Debug.Log("Player 2 Scored...");
                GameObject.Find("GameManager").GetComponent<GameManager>().Player2Scored;
            }
            else
            {
                Debug.Log("Player 1 Scored...");
                GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored;
            }
        }
    }
}

【问题讨论】:

  • 程序语句必须某事,即具有明显的副作用。检索Player2ScoredPlayer1Scored 属性值不会做任何事情,它只是获取那些然后立即丢弃的值。如果这些不是属性,那么您可能忘记了 调用 方法所需的名称后面的括号。查看重复项。

标签: c# unity3d pong


【解决方案1】:

这行代码什么也没做:

GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored;

它解析为一个值,但没有对那个值做任何事情。逻辑上等价于这样一行代码:

1;

从用法来看,看起来您打算记录该值。为此,您可以使用 Debug.Log(),就像您在上一行中所做的那样:

Debug.Log("Player 1 Scored...");
Debug.Log(GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored);

或者也许把它们放在同一行:

Debug.Log($"Player 1 Scored... {GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored}");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2020-04-08
    • 2016-06-27
    • 1970-01-01
    • 2023-03-05
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多