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. Dismiss Notice

Referencing Other Scripts

Discussion in 'Scripting' started by Crud41, Mar 13, 2015.

  1. Crud41

    Crud41

    Joined:
    Mar 13, 2015
    Posts:
    12
    Alright. So I have a prefab called "Hurdles" which has a script attached named "HurdleSelfDestruct", I also have a GameObject named Player who has a Player_Ball script attached. I need to access a boolean in the Player_Ball script from my HurdleSelfDestruct script. I have tried many different methods and nothing seems to work. I just keep getting this error:

    Assets/Scripts/HurdleSelfDestruct.cs(9,57): error CS0246: The type or namespace name `Player_Ball' could not be found. Are you missing a using directive or an assembly reference?

    Any help with this would be greatly appreciated.
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Does this work?
    Code (CSharp):
    1.  
    2. var myBool = GameObject.FindWithTag("Player").GetComponent<Player_Ball>().myBool
    3.  
     
  3. Crud41

    Crud41

    Joined:
    Mar 13, 2015
    Posts:
    12
    It is easier to accomplish in JS, I am using C# unfortunately.
     
  4. kippert

    kippert

    Joined:
    Mar 13, 2015
    Posts:
    3
    This is likely because your Player_Ball script isn't anywhere that your HurdleSelfDestruct script can see it.

    You may need something at the top of your HurdleSelfDestruct script along the lines of:
    using Player_Ball;

    However, I'm not convinced this will work because I don't know what namespaces you have set up.

    You may find it useful doing a short-ish tutorial on just standard c-sharp programming to get a feel for what is going on here. It's a bit more involved that the unity tutorials seem to let on.

    I hope this helps.
     
  5. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    My code is C# :p
     
  6. Crud41

    Crud41

    Joined:
    Mar 13, 2015
    Posts:
    12
    Oh, I have never used VAR before in C#, it always just throws me errors, so I just associated it with JS. My apologies :p
     
  7. Crud41

    Crud41

    Joined:
    Mar 13, 2015
    Posts:
    12
    I have solved the issue. Apparently I had an older script named Player, and for some reason it was calling that instead of the GameObject. After removing it, things started to work correctly. Thank you everyone!