Adding prefab objects to a list on another script/object?

Updated on May 15, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on May 13, 2018

Hi so I have a predicament, Im quite new to unity and coding in general, but I have been making a block stacking type game and so far I have been able to solve most problems.

But I have reached a point where I am completely stuck. So far I have blocks that spawn in at various spawn points above the camera and fall down with the option to move them left and right and then input ceases once they hit the floor or another block.

 

My issue is I wanted to have a height measuring system in place for a high score, the best way I could think of was to put all spawned objects into a list once they had hit another object. At the moment my BlockSpawner script sits on a game object in the scene and the blocks are prefabs that are not in the scene until the game runs. 

 

Now that’s the bit Im stuck on I wanted the blocks to add themselves to the list in a different script that is attached to a different object and it has to work for all prefab clones that spawn, but I have no idea how!! I have looked at many videos and forum posts about using variables from other scripts but nothing seems to work so far.

 

Sorry for the long post any In detail explanation would be appreciated or even links to other posts that answer the question would be very much appreciated 

 

Thank you 

  • Liked by
Reply
1 on May 14, 2018

You could look into raising a unity event when you spawn the new object. Pass the object into the event as an argument. Then you can have your other script listen for that event to be raised and once it has been raised you can add the object sent with that event to the list.

 

https://docs.unity3d.com/Manual/UnityEvents.html

on May 15, 2018

Thanks very much I did get a very nice detailed answer on unity questions that solved my problem, here is the link if anyone’s interested.

https://answers.unity.com/questions/1505714/adding-prefab-objects-to-a-list-on-another-script.html?childToView=1505811#answer-1505811

Show more replies
  • Liked by
Reply
Cancel