Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question about referencing one script in another script. (I think that's what I'm trying to do.)

Discussion in 'Scripting' started by Fyurien, Aug 16, 2015.

  1. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Ok, so I'm very new to both Unity and programming, so my vernacular may be very poor, but here's what I'm trying to do and here's what I'm getting.

    I have three scripts, an InputManager.cs (controls on screen touch controls), FirstPerson.cs (takes info from the InputManager and moves the player) and finally, camAnimation.cs (supposed to do a head bob on the camera whilst moving).

    That last part doesn't work. Here is the code...

    InputManager.cs FirstPerson.cs camAnimation.cs

    The error that I'm getting is "The name 'InputManager' does not exist in the current context"

    I've googled the snot out of this and still can't find anything that I can make sense of. As far as I can tell it has to do with making InputManager available for other scripts to access... but I have no clue how to do that.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    InputManager is in the TouchControlsKit namespace, and your other scripts aren't. You can reference it by writing "TouchControlsKit.InputManager" or you can include the entire TouchControlsKit namespace in your script by writing "using TouchControlsKit;" at the top of your script.

    Worth noting that the "FirstPerson.cs" script is actually the "FirstPersonExamples" class. This is going to cause problems. You should have the script name be the same as the class name. Also, it's in the "Examples" namespace, whereas your InputManager script and camAnimation are both in completely different namespaces. You should try to keep things a bit simpler, where possible.
     
    Fyurien likes this.
  3. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Hi Lysander,

    Thank you so much for the help. Ok, first things first, I fixed the name of FirstPerson to FirstPersonExample.

    Now to your explanation, I understand most of those words :)

    You said "You can reference it by writing "TouchControlsKit.InputManager"

    Where would I do this?

    Also you said "you can include the entire TouchControlsKit namespace in your script by writing "using TouchControlsKit;" at the top of your script"

    Same thing, where would I do this?

    Thank you very much!
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The former, anywhere you'd use "InputManager", you'd have to use "TouchControlsKit.InputManager". In this case, that means lines 42 and 43 in the camAnimation script. The latter, literally the very top of the camAnimation script (and any other script you'd want to reference classes in that namespace in, ie: where you'd want to use the InputManager class). You can see a couple of "using..." directives there already, just add it before or after those.

    If you do one, you don't have to do the other.
     
  5. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Did some thinking and I think I may have answered the first question. I should put "using TouchControlsKit" at the top of camAnimation.cs?
     
  6. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Holy crap that worked!!!!! I LOVE YOU MAN!!

    Thank you so much!
     
  7. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Ok, since you're so cool, I want to take full advantage of your help! :)

    My animations are triggered, but only once. So this is the code that triggers that animation...

    voidUpdate () {

    floatmoveX = InputManager.GetAxis( "Joystick", "Horizontal" );
    floatmoveY = InputManager.GetAxis( "Joystick", "Vertical" );

    if(moveX != 0 || moveY != 0){
    isMoving = true;
    }
    elseif(moveX == 0 && moveY == 0){
    isMoving = false;
    }

    CameraAnimations();

    }
    Maybe a setting in the animations themselves... right now they are set to Default. Maybe Loop?
     
  8. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Oopps.. nevermind, for some reason I had the animation set to Clamp Forever. Fixed it!

    Thank you again Lysander! :)
     
  9. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Btw, your game Verdant Nexus sounds very cool... a little bit like Dark Cloud for the PS2.