How can I spawn customers for my game and have them act in a manner similar to an example customer I have already created?

  1. Sorry for the long question but I’m basically creating an isometric 2.5D management game where the player oversees a restaurant and sets prices while customers come in to buy food and drinks.

  2. I already have a single customer that will find waypoints and go to them depending on its location but I’m having trouble spawning multiple customers into the scene . I have tried using a prefab but I cannot get it to save the values I have set for the public variables of the “SpawnCustomers.js” script and the “NavigateWaypoints.js” script.

  3. Also I would like to mention that I have tried duplicating the current game object but the problem is that each time there is a new duplicate it just copies the values that are on the original game object. For example if the original game object is heading towards waypoint 5 then the cloned object will also head towards waypoint 5 even if it didnt go to waypoint 1,2,3, or 4.

If there is a way to get a prefab to save specific values then I would like to know how to do it. [22157-these+are+the+prefab+values+that+wont+save.png|22157]

Above are the results I get if I try to save the values in the image below. [22159-these+values+wont+save.png|22159]

These are the values that should be applying to the new customer every time one is spawned in.

Thanks so much for taking the time to read all of that.

Your problem is that you cannot save in the prefab a reference to an item in the scene - because the prefab does not know what scene it will become a part of.

I would refactor the code so that the spawn points and the way points are discoverable on objects in the scene. E.g. create a manager component that is in the scene that holds the position arrays and have your prefabs find that manager object and get access to the information that they require.

If you think about this, it’s much more flexible, because if you make another scene then the same scripts can be applied.

There is a second way - don’t make them prefabs, have them in the scene but deactivated - you can still Instantiate a scene object to make a copy exactly the same way that you create an instance of a prefab - all it does is create a new object with exactly the same values as the one that acts as the source of the Instantiate call.

I agree with whydoidoit’s solution. You should create a new script that your prefabs will interact with to obtain all their variables when they are instantiated.

I’m not sure how much coding you’ve done with Unity so I’ll try to do a tiny example in case it helps.

//this is the script on the customer 

GameObject customerDataObject;

//example variables:
Transform spawnPoint1;
Transform spawnPoint2;

Transform customerWaypoint1;
Transform customerWaypoint2;


void Start()
{
//find the object that contains all the information
customerDataObject = GameObject.Find("Customer Data Object");

//call a function to set all the variables
getCustomerVariablesFunction();
}


void getCustomerVariablesFunction()
{
//random roll to see which spawn point

int randomSpawnPointRoll = Random.Range(1, 3);

if (randomSpawnPointRoll == 1)
{
spawnPoint1 = customerDataObject.GetComponent<CustomerDataScript>().spawn1;
}

else if (randomSpawnPointRoll == 2)
{
spawnPoint1 = customerDataObject.GetComponent<CustomerDataScript>().spawn2;
}

//get number of waypoints 
//could access array directly but easier like this for my example

customerWaypoint1 = customerDataObject.GetComponent<CustomerDataScript>().waypoint1;

customerWaypoint2 = customerDataObject.GetComponent<CustomerDataScript>().waypoint2;

}

So the “Customer Data Object” would be a prefab in all your scenes which contains the CustomerDataScript. You could just use public variables on the CustomerDataScript so that for each new scene you would drop in this prefab and set up the public variables. You could also do it without public variables and just use logic in that script when the scene starts to find everything automatically and then activate your customer prefabs. I would probably start by using a lot of public variables for this stuff so you can easily set things up and monitor the scenes in action. Can also set up public bools that will determine unique behaviors for different scenes.

That’s a pretty crude example particularly not using arrays or much logic at all. But hopefully that helps get you started if you are new to programming.