Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to get a script component of a gameobject[solved]

Discussion in 'Scripting' started by CodingBruh, May 3, 2016.

  1. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    I thought I tried this before and it said the type was invalid or something like that.
     
    hyouuu likes this.
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    if the script is attatched to the same gameObject you can do.

    Code (CSharp):
    1. gameObject.GetComponent<ScriptName>().variable
     
    dmendozam, autobr, nahuuy and 2 others like this.
  3. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Thanks! :)
     
  4. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    For performance reasons, store the script instance in a variable if you will use it more than once:

    Code (csharp):
    1.  
    2. ScriptName scriptName; // local variable to script instance in this object
    3.  
    4. void Awake()
    5. {
    6.     scriptName = gameObject.GetComponent<ScriptName>();
    7. }
    8.  
    9. void Update()
    10. {
    11.     scriptName.variable ...
    12. }
    13.  
     
    GuirieSanchez likes this.
  5. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Will do, thanks!
     
  6. nahuuy

    nahuuy

    Joined:
    Sep 21, 2022
    Posts:
    1
    Thanks <3
     
  7. VZPX

    VZPX

    Joined:
    Sep 5, 2021
    Posts:
    14
    what if we don't know where the script is attached to? Like is there a FindAllGameObjectsWithScript<Example>(); ?
     
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    This is basically its own question, and you should make your own thread instead of necroing one that is 6 years old.

    To answer it though, there is not anything like that. You could make your own static variable to store and access all references of it though, and have all of your scripts add themselves to it.

    Code (CSharp):
    1. public class Whatever : Monobehavior
    2. {
    3.   public static List<Whatever> WhateverList;
    4.  
    5.   void Start()
    6.   {
    7.     if(WhateverList == null)
    8.       WhateverList = new List<Whatever>();
    9.     WhateverList.Add(this);
    10.   }
    11. }
    And then you can get all of them by accessing
    Whatever.WhateverList