I thought I tried this before and it said the type was invalid or something like that.
1 Like
if the script is attatched to the same gameObject you can do.
gameObject.GetComponent<ScriptName>().variable
5 Likes
Thanks!
For performance reasons, store the script instance in a variable if you will use it more than once:
ScriptName scriptName; // local variable to script instance in this object
void Awake()
{
scriptName = gameObject.GetComponent<ScriptName>();
}
void Update()
{
scriptName.variable ...
}
1 Like
Will do, thanks!
Thanks <3
what if we don’t know where the script is attached to? Like is there a FindAllGameObjectsWithScript(); ?
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.
public class Whatever : Monobehavior
{
public static List<Whatever> WhateverList;
void Start()
{
if(WhateverList == null)
WhateverList = new List<Whatever>();
WhateverList.Add(this);
}
}
And then you can get all of them by accessing Whatever.WhateverList