- Home /
Attached scripts as List?
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]()
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.
Answer by Ves · May 01, 2011 at 03:23 PM
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
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# NullReferenceException 2 Answers
Can't add GameObjects to a List 2 Answers
Adding scripts to items in a list 1 Answer