【问题标题】:C# Instantiate Vector3 ProblemsC#实例化Vector3问题
【发布时间】:2023-02-02 15:24:06
【问题描述】:

Instantiate(o, new Vector3(LocateX, LocateY, 0), transform.rotation); 在此代码中,Visual Studio 2022 不断告诉我 LocateX 和 LocateY 有错误... 请帮我 使用统一 错误代码:CS0165

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class OXScript : MonoBehaviour
{
    private int clickNumber;
    public GameObject o;
    public int spotNumber;

    // Start is called before the first frame update
    void Start()
    {
        clickNumber= 0;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void SpawnO()
    {
        //  1,   2,  3, y = 3
        //  4,   5,  6, y = 0
        //  7,   8,  9, y = -3
        // x=-3 x=0 x=3

        float LocateX;
        float LocateY;
        int ix = 1;
        for(int i=1; i<10; i++)
        {
            if (clickNumber == 0 && spotNumber == i)
            {
                // y
                if(1 <= i || i <= 3 )
                {
                    LocateY = 3;
                } else if(4 <= i || i <= 6)
                {
                    LocateY = 0;
                } else if (7 <= i || i <= 9)
                {
                    LocateY = -3;
                }
                //x
                if (ix == i)
                {
                    LocateX = -3;
                    ix = ix + 3;
                } else if (ix + 1 == i)
                {
                    LocateX = 0;
                    ix = ix + 3;
                } else if (ix + 2 == i)
                {
                    LocateX = 3;
                    ix = ix + 3;
                }

                Instantiate(o, new Vector3(LocateX, LocateY, 0), transform.rotation);
                spotNumber++;
            }
        }
    }
}

我尝试将 LocateX 和 LocateY 更改为 int,然后再次更改了 float。 我不知道如何解决它。

【问题讨论】:

  • 请提供精确的错误消息而不仅仅是“LocateX 和 LocateY 有错误”。

标签: c# unity3d visual-studio-2022 unityscript


【解决方案1】:

使用局部变量前需要初始化。

    float LocateX = 0;
    float LocateY = 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2014-01-25
    相关资源
    最近更新 更多