x


Bool not returning correct value from getCompoent

I'm trying to pass a value from a player script to the camera script, to see if the player had ducked to move the camera. I've tested the bool in the player script and it returned the correct value but when I pass the bool to the camera script it constantly returns false.

  void PlayerDuck()
    {
       if(headpos.y < duckThreshold)
       {
         duck = true;
         //Debug.LogError("ducking ");
       }
       else
       {
         duck = false;
         //Debug.LogError("not ducking ");
       }

but in the cam script it keeps returning false in the update method after i init the variable in the start method.

_ducking = GameObject.Find("player").GetComponent<Duck>().duck;

and the update method constantly shows it as false

void Update () {


       Debug.LogError(_ducking);
    }
more ▼

asked Feb 01 '12 at 09:36 AM

dirtydemon gravatar image

dirtydemon
35 2 6 8

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

1 answer: sort voted first

If you're calling this "_ducking = GameObject.Find("player").GetComponent().duck;" in start, then you're only getting what the variable is AT Start....

cache the script, then get the variable in update and see how that goes.

void Start(){
   _ducking = GameObject.Find("player").GetComponent<Duck>() ;
}

void Update(){
   Debug.Log(_ducking.duck) ;
}

Just a quick guess, anyway... never did it like that before (setting the variable with getcomponent at start... I always do it this way and it works)

more ▼

answered Feb 01 '12 at 09:40 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 10 39

I tried what you suggested and it didn't work, it wont recongnise the _ducking.duck because it cant see the .duck being pulled from the Duck class.

Feb 01 '12 at 09:51 AM dirtydemon

I just tried it after seeing your comment, and it works perfectly fine. Did you change your _ducking variable declaration to suit the other changes?

NameOfDuckScript _ducking ;

Feb 01 '12 at 10:03 AM Lo0NuhtiK

Hmm thanks for the support. I changed that and now i'm just getting a null refrance exception form the debug statement. saying its not linked to an object. Even though when I hover over it in mono it shows me it as being a bool???? sorry the exception error is coming from _ducking = GameObject.Find("player").GetComponent();

Feb 01 '12 at 10:14 AM dirtydemon

Oh... didn't see the edit you made to your comment. So, you got it to work now then?

Feb 01 '12 at 10:24 AM Lo0NuhtiK

Unfortunatly no, I'm a uni student and got a demo in 90 mins wanna get the ducking working by then. just gonna plug away at it.

Feb 01 '12 at 10:27 AM dirtydemon
(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:

x4374
x422
x249

asked: Feb 01 '12 at 09:36 AM

Seen: 361 times

Last Updated: Feb 01 '12 at 11:11 AM