Ves
1
Hey everyone,
Is there a way to create a list of all the attached scripts on an object? I have tried several ways:
-
Creating a global list of type MonoBehavior and attempting to populate it as below:
public scriptList as List[of MonoBehaviour]
def Start():
go as (MonoBehavior) = GetComponents[of MonoBehavior]()
<pre>` for item in go:
scriptList.Add(item as MonoBehavior)
`
But I get a 'NullReferenceException: Object reference not set to an instance of an object' and can't seem to find a way around it.
-
Manually creating a list
addedScript as ExampleScript
addedScript = GetComponent[of ExampleScript]()
scriptList as List = [addedScript]
However, I cannot reference in the
item in the list by doing something
like
scriptList[0].someVariable
because it gives me a 'BCE0019:
'someVariable' is not a member of
'object'.' error.1.
Does anyone know of a way to do this?
Thanks!
1.
Ves
2
It seems the issue can be solved using the second method in my first post. Except Lists are not the way to go, instead, just use arrays:
addedScript1 as ExampleScript1
addedScript1 = GetComponent[of ExampleScript1]()
addedScript2 as ExampleScript2
addedScript2 = GetComponent[of ExampleScript2]()
scriptArray = (addedScript1, addedScript2)
Where now if someVariable is a boolean, it will actually return true or false.
scriptArray[0].someVariable