x


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?
more ▼

asked May 20 '12 at 10:30 PM

Gilead7 gravatar image

Gilead7
117 18 33 42

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
   }
}
more ▼

answered May 21 '12 at 01:42 AM

whydoidoit gravatar image

whydoidoit
33.6k 14 23 105

Thanks Mike, 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?

May 21 '12 at 09:00 PM Gilead7

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?

May 21 '12 at 10:45 PM whydoidoit

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

state=ChestState.closed;

} public void OnMouseUp(){ 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;
    Messenger<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.

May 25 '12 at 04:12 PM Gilead7

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

May 25 '12 at 05:47 PM whydoidoit

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!

May 26 '12 at 06:49 PM Gilead7
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4372
x372
x336
x241
x7

asked: May 20 '12 at 10:30 PM

Seen: 565 times

Last Updated: May 31 '12 at 11:15 PM