- Home /
Switch statement not working C# (answered)
My game creates two obstacles which can each be in one of three different positions(picked randomly). However in my game i never want them to be the same position to begin with. So using my knowledge of switch statements from the switch statement beginner tutorial i created one to change my second obstacles position to a position which was not the same as the first and was randomly decided from the other two positions. I know this sounds complex but hopefully the code will explain it, i come across what i believe are syntax errors yet the code is fine in comparison with the rules talked about in the tutorial. Below is my code and errors -
using UnityEngine;
using System.Collections;
public class Generater2 : MonoBehaviour {
public Rigidbody original;
public new Vector3 startPoin;
public Vector3 posOne;
Vector3[] positionArra = new Vector3[3];
Vector3[] positionArr = new Vector3[3];
// Use this for initialization
void Start () {
InvokeRepeating("Spawn", 2.1f, 1f);
positionArra[0] = new Vector3(0f,4f,100f);
positionArra[1] = new Vector3(10f,4f,100f);
positionArra[2] = new Vector3(-10f,4f,100f);
positionArr[0] = new Vector3(0f,4f,100f);
positionArr[1] = new Vector3(-10f,4f,100f);
}
// Update is called once per frame
void Update ()
{
startPoin = positionArra[Random.Range(0,3)];
GameObject plane = GameObject.Find("Plane");
Generater playerScript = plane.GetComponent<Generater>();
posOne = playerScript.startPoint;
if (posOne == startPoin)
{
switch (posOne)
{
case (-10f, 4f, 100f):
startPoin = positionArra[Random.Range(0,2)];
break;
case (0f, 4f, 100f):
startPoin = positionArra[Random.Range(1,3)];
break;
case (10f, 4f, 100f):
startPoin = positionArr[Random.Range(0,2)];
break;
}
}
}
void Spawn () {
Rigidbody clone = (Rigidbody)Instantiate (original, startPoin, new Quaternion (0,0,0,0));
}
}
Assets/Scripts/Generater2.cs(36,43): error CS1026: Unexpected symbol ,', expecting
)'
Assets/Scripts/Generater2.cs(40,36): error CS1525: Unexpected symbol `case'
Assets/Scripts/Generater2.cs(54,14): error CS0116: A namespace can only contain types and namespace declarations
Assets/Scripts/Generater2.cs(58,1): error CS8025: Parsing error
Assets/Scripts/Generater2.cs(44,36);error CS1525: Unexpected symbol 'case'
Answer by DoTA_KAMIKADzE · Aug 15, 2015 at 04:05 PM
Switch statement doesn't have a built-in functionality for that. Switch expression or case label must be a bool, char, string, sbyte, byte, short, ushort, int, uint, long, ulong, enum, or corresponding nullable type.
You can either create a custom "switch" or just use if-else, e.g.:
if (posOne == startPoin)
{
if (posOne == new Vector3(-10f, 4f, 100f)) startPoin = positionArra[Random.Range(0, 2)];
else if (posOne == new Vector3(0f, 4f, 100f)) startPoin = positionArra[Random.Range(1, 3)];
else if (posOne == new Vector3(10f, 4f, 100f)) startPoin = positionArr[Random.Range(0, 2)];
}
Thank you very much, my code works and runs smoothly. I am understand that my situation may have been complex to understand.
Your answer
Follow this Question
Related Questions
[Solved]List.FindIndex error C# 1 Answer
Method Must have a Return Type (Vector3 x,y,z) 2 Answers
Switch statement error 1 Answer
Help with AI script 1 Answer