Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Gilead7 · May 20, 2012 at 10:30 PM · c#distancetagswitchcase

Updated Question if statement not executing case

I'm trying a different approach. The Raycasting was simply not working, so I tagged the FPController as Player and tried it this way. I reset the max distance to 8.5f since you can never get to 2.0 even if you are right on top of the chest. While I get the distance, the case statement won't execute. I'm guessing because of the return, but I'm not sure how to make them both happy. In Start() I'm setting the variable- Player= GameObject.FindGameObjectWithTag("Player"); if(Player==null) Debug.Log("Could not find Main Camera");

 public void OnMouseUp(){
     Debug.Log("Mouse Up");


                  if(Vector3.Distance(transform.position, Player.transform.position)>maxDistance)
         Debug.Log("You need to be closer!");
         Debug.Log(Vector3.Distance(transform.position, Player.transform.position));        
     return;
     if(Vector3.Distance(transform.position, Player.transform.position)<=maxDistance)
 
     switch(state){
         case ChestState.open:
         state=ChestState.inbetween;
         StartCoroutine("CloseChest");
         break;
     
         case ChestState.closed: 
             StartCoroutine("OpenChest");
         break;
         }
     }
 Thoughts? Anyone?
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
0

Answer by whydoidoit · May 21, 2012 at 01:42 AM

I think you need braces around those Debug.Log statements - I presume that you were intending to do that logging and return if the distance was too far away - but the way the code reads now it will only use the If for the first debug.log. The second debug.log will always happen and then it will always return.

Here's the code you should use - you also don't need the second if.

 public void OnMouseUp()
 {
     Debug.Log("Mouse Up");


    if(Vector3.Distance(transform.position, Player.transform.position)>maxDistance) 
    {
        Debug.Log("You need to be closer!");
        Debug.Log(Vector3.Distance(transform.position, Player.transform.position));     
       return;
    }

    switch(state)
    {
        case ChestState.open:
            state=ChestState.inbetween;
            StartCoroutine("CloseChest");
            break;

        case ChestState.closed: 
            StartCoroutine("OpenChest");
            break;
    }
 }
Comment
Add comment · Show 17 · 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 Gilead7 · May 21, 2012 at 09:00 PM 0
Share

Thanks $$anonymous$$ike, however for whatever reason, it will not execute the case/switch no matter what the distance is. I copied and pasted your code right in. Any further thoughts?

avatar image whydoidoit · May 21, 2012 at 10:45 PM 0
Share

Hmmm, I guess that if is always true then? What kind of values is maxDistance ? What is state and where is it defined? Is it perhaps that it never has either of those values?

avatar image Gilead7 · May 25, 2012 at 04:12 PM 0
Share

public float maxDistance=8.5f; void Start () {

 state=ChestState.closed;

} public void On$$anonymous$$ouseUp(){ switch(state) { case ChestState.open: state=ChestState.inbetween; ForceClose(); break;

    case ChestState.closed: 
         if(myGUI.chest !=null){
             myGUI.chest.ForceClose();
         }
        StartCoroutine("OpenChest");
        break;

} } private IEnumerator OpenChest(){ myGUI.chest=this; inUse=true; audio.PlayOneShot(openSound); yield return new WaitForSeconds (1); PopulateChest(5);

     state=ChestState.open;
     $$anonymous$$essenger<int>.Broadcast("DisplayLoot");
 }

These are the relevant parts showing states. It has been updated, but still doesn't work. No matter what I do, the distance check breaks it.

avatar image whydoidoit · May 25, 2012 at 05:47 PM 0
Share

Right next up - what is the distance in the log?

avatar image Gilead7 · May 26, 2012 at 06:49 PM 0
Share

The distance does update with every click, but I've been at 4.0 from the chest(right on top of it) and it will still not open the silly thing. I have no idea what is wrong. Thanks!

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting dynamic indicies from multiple dropdown menus 0 Answers

Saving drop down menu selection 1 Answer

Why does only one object respond, while more objects have exact same script? 0 Answers

Why would my switch's case be correct but the output is not? 2 Answers

Adjusting location on save to avoid infinite loop. 1 Answer


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