0
votes

Fairly new to Unity, but quickly learning.

I have a spawner which is an empty GameObject which spawns balloons. The balloon object which spawns has a movement script applied which follows waypoints.

The issue I am having is that I can't click and drag the waypoints (also empty game objects) onto the public variables, I can only do this if the balloons have been added into the game Hierarchy first, but I'm spawning the prefab which has the script applied as I don't want it to be in the game yet. If I add the prefab into the game first, I can set the waypoints to the game objects.

How can I fix this so that I am able to add the waypoint game objects to the prefab in the assets?

1
Hi! I think you should head over to the Unity Q&A page. This should answer your question. Essentially, you should set the attributes you would normally set through the inspector programmatically wherever it is in your code that you are instantiating the GOs.Max von Hippel

1 Answers

2
votes

you can have this method in movement script:

public void SetWayPoints(Transform[] waypoints)
{
    this.wayPoints = waypoints;
}

And when you instantiate your prefab just get the component and set the way points;

GameObject baloon = Instantiate(baloonPrefab) as GameObject;
var movementScript = baloon.GetComponent<Movement>();
if(movementScript != null)
{
    movementScript.SetWayPoints(waypoints);
}

Note that above code is just an example to illustrate the method of doing it. You script can vary depending on your implementation of waypoints. I assumed that waypoints is an array of transform within a movement script and baloon follows it one by one. Also spawner class have a reference of waypoints in hierarchy and you just pass that array from spawner.

Hope this helps. Let me know if you have any problem.