Search Unity

Formatting Help

Discussion in 'Scripting' started by nm8shun, Apr 4, 2009.

  1. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    So I want the script to check 'go' and see if it has MouseLook enabled. If it does not, the script should turn it on.

    If it's on, it should do nothing.

    This is the code I've been fighting with. I know there has to be some formatting error here that's just beyond my skills. Could I get a little help on my syntax? Thanks!

    Code (csharp):
    1. if
    2.             (go.GetComponent("MouseLook").enabled(false)){
    3.             go.GetComponent("MouseLook").enabled=true;
    4.             }
    5.  
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Code (csharp):
    1. if (!go.GetComponent("MouseLook").enabled) {
    2.    go.GetComponent("MouseLook").enabled=true;
    3. }
    Alternatively:

    Code (csharp):
    1. if (go.GetComponent("MouseLook").enabled == false) {
    2.    go.GetComponent("MouseLook").enabled=true;
    3. }
     
  3. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Awesome. Thanks much.

    That kind of thing isn't really in the documentation anywhere, and I kind of have to discover them through tearing apart other people's scripts. Slow going for us artist folks.

    I really appreciate your help!
     
  4. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    So, update on this. I modified things just a bit to be:

    Code (csharp):
    1.     if (!Maincamparent.GetComponent("MouseLook")){
    2.             Maincamparent.transform.rotation = Quaternion.identity;
    3.             Maincamparent.AddComponent ("MouseLook");
    4.         }  
    Thinking that this would tell it to go see if it could find the MouseLook component on Maincamparent - and if not, to orient the camera, and add MouseLook again.

    Unfortunately, it's orienting the camera even if MouseLook is already there...it's ignoring the 'if' part.

    What am I doing wrong?