Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by g__l · Aug 15, 2015 at 04:00 PM · c#errorvector3switch

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'

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)];
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image g__l · Aug 15, 2015 at 04:12 PM 0
Share

Thank you very much, my code works and runs smoothly. I am understand that my situation may have been complex to understand.

avatar image ProbePLayer · Dec 15, 2015 at 06:32 PM 0
Share

Thanks for clearing this up.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vector3 multiplied by its magnitude results in infinity? 1 Answer

n object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)' 1 Answer

error CS0122: `CharacterMotorC.SetVelocity(UnityEngine.Vector3)' is inaccessible due to its protection level 1 Answer

What is the error? 2 Answers

Vector3.SmoothDamp has some invalid arguments 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges