简体   繁体   English

我缺少什么统一/C#?

[英]What am i missing unity/c#?

so i am trying to code a unity game where you can build a LEGO like bench with several stones.所以我正在尝试编写一个统一游戏,您可以在其中用几块石头构建一个像 LEGO 一样的长凳。 But i am new to coding so i got a lot of errors.但是我是编码新手,所以我遇到了很多错误。

  1. The else in the end is triggered everytime and i dont know why, even if an else if is true before.最后的 else 每次都会触发,我不知道为什么,即使 else if 之前是真的。
  2. the platzierteSteine int will not increment and always 0 when i tried to print it out.当我尝试打印它时, platzierteSteine int 不会增加并且始终为 0。
  3. Is the "||"是“||” condition correctly used?条件正确使用?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrabScript : MonoBehaviour {
    /// <summary>
    /// GameObject, das den Spieler-Controller beschreibt
    /// </summary>
    [SerializeField] GameObject controller;

    int[] steineInEbene = {2, 4, 3, 2, 1};
    int platzierteSteine = 0;
    int ebene = 1;

    public GameObject falsch;
    public GameObject richtig;

    int zähler = 0;

    /// <summary>
    /// Start is called before the first frame update
    /// </summary>
    void Start() {  

        // Fehlermeldung wird ausgeblendet
        falsch.SetActive(false);
        // Siegermeldung wird ausgeblendet
        richtig.SetActive(false);

        }

    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update() {

        // Falls die Distanz zu diesem Objekt passt, weitermachen. Sonst passiert nichts.
        if (Vector3.Distance(controller.transform.position, this.transform.position) < 2.5f) {
            // Prüfe, ob die Taste E gedrückt wird
            // Starte halten des Stein
            if (Input.GetKeyDown(KeyCode.E))
            {
                // Parent-Objekt festlegen. Siehe Konzept des Scenegraphen aus der Vorlesung!
                this.transform.parent = controller.transform.GetChild(0).transform;
                // Während des Haltens sollen keine Kräfte, Kollisionen usw. wirken
                this.GetComponent<Rigidbody>().isKinematic = true;
                this.GetComponent<Collider>().enabled = false;
                controller.GetComponent<Collider>().enabled = true;

            }
            
            // Wenn der Controller auf der Platte steht packt er die Steine dahin wo sie hingehören 
            if (400 <= controller.transform.position.x && controller.transform.position.x <= 410 && 
                540 <= controller.transform.position.z && controller.transform.position.z <= 550) 
            {

                if (Input.GetKeyDown(KeyCode.F))
                {

                    if(this.transform.name == "1x2" && ebene == 1) 
                    {
                        this.transform.position = new Vector3(407.97f, 0.44f, 547.08f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[0]) { ebene++; platzierteSteine = 0; } 
                    }

                    else if(this.transform.name == "1x22" && ebene == 1) 
                    {
                        this.transform.position = new Vector3(407.97f, 0.44f, 542.87f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[0]) { ebene++; platzierteSteine = 0; } 
                    }

                    else if((this.transform.name == "1x1" || this.transform.name == "1x12" || this.transform.name == "1x13" || this.transform.name == "1x14") && ebene == 2) 
                    {
                        if(zähler == 0) {
                            this.transform.position = new Vector3(407.97f, 1.04f, 547.08f);
                        }
                        else if(zähler == 1) {
                            this.transform.position = new Vector3(407.97f, 1.04f, 542.87f);
                        }
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        zähler++;
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[1]) { ebene++; platzierteSteine = 0; } 
                    }

                /*  else if(this.transform.name == "1x12" && ebene == 2) 
                    {
                        this.transform.position = new Vector3(407.97f, 1.04f, 542.87f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[1]) { ebene++; platzierteSteine = 0; } 
                    }
                */
                    else if(this.transform.name == "Schräg" && ebene == 2) 
                    {
                        this.transform.position = new Vector3(407.37f, 1.04f, 547.08f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 90);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[1]) { ebene++; platzierteSteine = 0; } 
                    }

                    else if(this.transform.name == "Schräg2" && ebene == 2) 
                    {
                        this.transform.position = new Vector3(407.37f, 1.04f, 542.87f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 90);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[1]) { ebene++; platzierteSteine = 0; }
                    }

                    else if((this.transform.name == "1x1" || this.transform.name == "1x12" || this.transform.name == "1x13" || this.transform.name == "1x14") && ebene == 3)  
                    {
                        if(zähler == 2) {
                            this.transform.position = new Vector3(407.97f, 1.64f, 547.08f);
                        }
                        else if(zähler == 3) {
                            this.transform.position = new Vector3(407.97f, 1.64f, 542.87f);
                        }
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        zähler++;
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[2]) { ebene++; platzierteSteine = 0; }
                    }

                /*  else if(this.transform.name == "1x13" && ebene == 3) 
                    {
                        this.transform.position = new Vector3(407.97f, 1.64f, 542.87f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[2]) { ebene++; platzierteSteine = 0; } 
                    }
                */
                    else if(this.transform.name == "sitz" && ebene == 3) 
                    {
                        this.transform.position = new Vector3(407.37f, 1.44f, 547.08f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[2]) { ebene++; platzierteSteine = 0; } 
                    }

                    else if(this.transform.name == "1x1 mit Noppe 2" && ebene == 4) 
                    {
                        this.transform.position = new Vector3(407.969f, 2.24f, 547.081f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[3]) { ebene++; platzierteSteine = 0; }
                    }

                    else if(this.transform.name == "1x1 mit Noppe" && ebene == 4) 
                    {
                        this.transform.position = new Vector3(407.969f, 2.24f, 542.87f);
                        this.transform.rotation = Quaternion.Euler(270, 0, 0);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[3]) { ebene++; platzierteSteine = 0; }
                    }

                    else if(this.transform.name == "rückseite" && ebene == 5) 
                    {
                        this.transform.position = new Vector3(407.54f, 2.24f, 547.08f);
                        this.transform.rotation = Quaternion.Euler(0, 270, 90);
                        platzierteSteine++;
                        if(platzierteSteine == steineInEbene[4]) { ebene++; platzierteSteine = 0; richtig.SetActive(true); }
                    }

                    else
                    {
                        falsch.SetActive(true); 
                        StartCoroutine("Wait"); 
                    }
                    
                this.transform.parent = null;
                this.GetComponent<Collider>().enabled = true;
                controller.GetComponent<Rigidbody>().isKinematic = true;    
                    
                }
            }

            // Wenn der Controller nicht auf der Platte steht lässt er die Steine einfach fallen
            else if (Input.GetKeyDown(KeyCode.F)) { 

                this.transform.parent = null;
                this.GetComponent<Collider>().enabled = true;
                this.GetComponent<Rigidbody>().isKinematic = false;
                controller.GetComponent<Rigidbody>().isKinematic = true;

            }
        }
    }

    
    IEnumerator Wait() 
    {
        yield return new WaitForSeconds(3.5f);
        falsch.SetActive(false);
    }
}

1.- The else in the end is triggered everytime and i dont know why, even if an else if is true before. 1.-最后的else每次都会触发,我不知道为什么,即使else if之前是真的。

It is inside the Update .它在Update里面。 As the comment in the code it is colled every frame, so what is inside this, is continously being called.作为代码中的注释,它每帧都会被收集,所以它里面的内容会被不断地调用。

2.- the platzierteSteine int will not increment and always 0 when i tried to print it out. 2.- platzierteSteine int 不会增加,当我尝试打印它时始终为 0。

That is because any f the conditions of your if where platzierteSteine++;那是因为你的if where platzierteSteine++; is is being met.正在满足。 You need to debug and check taht out.您需要调试并检查。 Check this to see how to debug, set breakpoints and check the value of your conditions to see if they are bieng met and why.检查以了解如何调试、设置断点并检查条件的值以查看它们是否满足以及为什么。

3.- Is the "||" 3.- 是“||” condition correctly used?条件正确使用?

That is the "or" logical operator.那是“或”逻辑运算符。 It is correctly used regarding compilation.它在编译方面被正确使用。 However is the logic you expect is not occurring try debugginh that out.但是,您期望的逻辑不会发生,请尝试调试它。

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

相关问题 C#/WPF:这个回调我错过了什么? - C#/WPF: What am i missing with this callback? c# 中的递归加载,我错过了什么? - Recursive load in c#, what am I missing? C# 无法识别 Else 语句,我缺少什么? - Else statement is not recognized in C# what am I missing? 表现怪异的transform.lookat或我错过了一些东西(unity3d C#) - transform.lookat behaving weirdly OR I am missing something (unity3d c#) Unity对象参考+序列化字段。 我想念什么? - Unity object reference + serialized field. What am I missing? 在C#中使用数据集时,datagridview上没有数据出现,我在这里缺少什么? - No data appear on my datagridview when using dataset in c#, what am i missing here? 我缺少什么将我的 C# 代码连接到 MySQL 数据库 - what i am missing to connect my C# code to MySQL database 我的C#到PHP代码转换中没有sha1匹配项,我缺少什么? - Not getting a sha1 match in my C# to PHP code conversion, what am I missing? c#WPF&#39;名称xxx在名称空间中不存在-我缺少什么? - c# WPF 'The name xxx does not exist in the namespace - what am i missing? ASP.NET C# OData 服务 + 导航属性 + $expand = null。 我错过了什么? - ASP.NET C# OData Service + Navigation Property + $expand = null. What am I missing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM