Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tyrebear · Feb 14, 2014 at 04:20 AM · prefabsavingvaluesclones

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. alt text

Above are the results I get if I try to save the values in the image below. alt text

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.

these values wont save.png (60.2 kB)
these are the prefab values that wont save.png (54.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Feb 14, 2014 at 04:32 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tyrebear · Feb 15, 2014 at 06:28 PM 0
Share

I like option two and I just made a static object containing the script I wanted. Now I need to figure out how to make customers wait in lines. Thanks for your help because you solved my problem.

avatar image
0

Answer by RyanZimmerman87 · Feb 14, 2014 at 05:01 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ingame prefab saving and manipulation 0 Answers

Pressing Apply Breaks Prefab 1 Answer

Dynamically saving and loading player-created prefabs 0 Answers

Allowing users to create customizable characters 1 Answer

How do you save the position of clone prefabs in a text file? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges