Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Referencing custom classes (scripts) in Monodevelop from Unity project assets?

Discussion in 'Scripting' started by emilnyb, Dec 18, 2014.

  1. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    Hey, I'm wonder, what the heck do I need to do to be able to reference the classes and their methods I create for other game objects and scene elements in some script in Monodevelop.

    Because I'm like, I have this class "A" (theoretical situation), and I'm writing a script for it that depends on some other variable in another class "B" (script) for another game object element. And I want to be able to have that variable as a parameter for my methods in class "A".

    The question arose when I was trying out the "Roll-a-Ball" tutorial which I only need 1 more episode to complete. The manner of dragging game object elements onto a variable in the Unity editor of another game object element seems so messy, and is just not very satisfactory. I just want to be able to make a call to that other class "B"s value in the script for class "A" that I'm writing. In the tutorial the example is between a "Text variable" (or a GUIText variable though for newer unity versions one must use Text) in the Player Controller, and a game object element of the Text type.

    In that example I would like to just make a function call to the text game object element and ask it to update itself in the Player Controller script... anyone knows how to make this happen?
     
  2. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    When you start typing the name of a class you've made that is somewhere in your project, the auto-complete should show it. If it doesn't restart mono because it can bug out occasionally. If that doesn't fix it, check and make sure that you're not trying to reference a C# script in a JS script or the other way around. There are workarounds to make this happen, but you can't do both. You either move JS into a special folder (standard assets, I think) so C# can access it, or you don't and then only JS can access C# (might have that backwards, but you get the idea).

    If you're only using JS or C# and none of the above is working, try closing mono and opening it by double clicking a script in your unity project instead of opening mono directly. If there's still any trouble, please post more information and maybe some sample code. Good luck.
     
  3. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    But what if the same script is used in different game object elements, how does the script know which one it is? How do I make a call to a class specififying where which game object element is using it?
     
  4. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    is perhaps the answer that I should only have one script for each game object element if the values I'm retrieving are specific to that game object element?
     
  5. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
  6. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
  7. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    Haha sorry. On my phone and suck at typing.
    Another thing: Getcomponent is somewhat performance expensive. Do not constantly call it unless you have to (store as reference)

    Edit: or any of those get *blank* functions
     
  8. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    Thnx, shall try to remember that x)
     
  9. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    Another question:

    This seems to answer my next question of how to access child elements of a game object, but I don't see the game object being specified in this example. Would I first have to call the game object as in the unity GameObject.Find example, and then replace "gameObject.GetComponent<type>();" with "variable_holding_game_object_reference.GetComponent<type>();"?

    In that case, would I have to name all my components uniquely I suppose? Because for instance in the "Roll-a-Ball" example project tutorial, there's a game object called "Pickups" which has tons of "Pickup" children, and I don't see how else for the "gameObject.GetComponent<type>();" call to figure out the difference between those then if I typed "variable_for_Pickups_game_object.GetComponent<Pickup>();" as there's like a dozen "pickup" components to choose from
     
  10. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    gameobject.transform.getchild(# of child).getcomponent<type>();
     
  11. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
  12. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    Wait now... I'm not following, isn't component and child the same thing? What's the difference?
     
  13. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    Children are game objects in the hierarchy. Components are scripts and other things part of the game object(Not sure if explained properly)
     
  14. emilnyb

    emilnyb

    Joined:
    Jan 9, 2013
    Posts:
    28
    oh, a game object of another game object, okay.