Question: How do I pass params to runtime instatiated objects?


I am trying to get creative with the block-breaker game, and am using a number of block variants, some of which spawn blocks of a different type when they are destroyed. The spawning part of the game instantiating new block types in specific locations with the same rotation as the spawning block works fine.

I have a mechanism where the score for destroying each block is calculated from a variable blockValue and a blockValueMultiplier that is a serialized Field in the Inspector.

The initial block variants I am using to spawn new block variants each have a blockValue calculated by a specific instance of another GameObject. I want to pass either the identifier for the reference GameObject instance or the current blockValue to the spawned blocks.

The problem I have is that the new blocks do not have an obvious association with the reference GameObject used by the spawning initial block or the spawning block itself. For blocks that are on screen when the scene is opened, I can set the association using [SerializeField] in the inspector, but I cannot do that for blocks that are spawned at run-time.

I need a way to tell the spawned block which instance of the reference GameObject holds the specific blockValue that it should use. If that is not possible, I want to pass the blockValue from the spawning block to the new block.

The Unity script reference for Instantiate says that you can pass the Original Object to be cloned, a Vector3 position, a rotation and a parent Transform, but it gives no examples of the use of the parent Transform. I have tried using the reference GameObject set for teh spawning block by the Inspector using [SerializeField] as the Transform parent, but Visual Studio gives me an error saying that I cannot convert from the GameObject Type to UnityEngine.Transform.

Any pointers appreciated, thanks in advance.

Hi Trevor,

Have you already completed the Laser Defender section? Rick uses scriptable objects to configure enemy waves and creates a class that processes these scriptable objects to instantiate game objects.

You could do something similar.

Thanks, Nina

No, I haven’t got that far in the course yet. It sounds like I might be getting ahead of myself on the coding front. I’ll run with the random block value method that I have used as a placeholder for the block-breaker game so that i can publish it for comment and move on. I’ll come back to it later if I decide to take the block-breaker further.

Rather than mark this as solved, can I mark it as ‘On Hold’ to see if the later part of the course solves it, or is that against the guidelines?

I could close your thread, and if you want to re-open it, you could message me or @Marc_Carlyon. Alternatively, we could leave it open for a month or two.

OK, let’s leave it open for now, please, Nina. If I don’t get to the relevant point in the course by early January so that I can test your suggestion and see if that is the resolution, we can close it then by dint of my inactivity.

I also have a couple of ideas that I want to try from reading the C# reference manual. However, as that led me towards scriptable objects, which looked a bit daunting without Rick’s guidance through the minefield, I suspect that your answer is likely to be the right one.


My answer is not “the right one”. There are probably other solutions, maybe even better ones. However, this was the first one I came up with.

Please feel free to ask our helpful community of students for advice in our official Discord chat. :slight_smile:

This topic was automatically closed after 68 days. New replies are no longer allowed.

Privacy & Terms