[英]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.但是我是编码新手,所以我遇到了很多错误。
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.