Joel
Joel

Reputation: 27

Unity Array and Instatiation

Im new in unity and I have a problem regarding arrays. Here is the editor snapshot

I want to be able to set the Elements of the arrays via script. Here straight is a "public gridElementScript[] straight;"

Upvotes: 0

Views: 72

Answers (1)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

If I'm understanding correctly, you're asking how to access Straight from another instance. If so, you need to cast your instantiated prefab to a GameObject and call GetComponent<MyComponent>() on it. Something like I've written below should work for you.

GameObject object = (GameObject)Object.Instantiate(myPrefab)
GridElementScript script = object.GetComponent<GridElementScript>();
script.Straight[0] = neighbor

Upvotes: 1

Related Questions