Search Unity

Multiple cars

Discussion in 'Editor & General Support' started by carking1996, Feb 15, 2011.

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hello, I want my game to have multiple cars. how would I do that? :confused:

    thanks
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Maybe 'prefabs' are what you're looking for. (You can also duplicate game objects directly in the hierarchy.)
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    No, I mean, How can I make the game have multiple cars to select, and each have different tuning?
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Oh. Well, you didn't say anything about that in your original question ;)

    Anyway, there's a fair amount to what you describe above, and there's lots of different ways you can do it. You'll need a user interface of some sort (e.g. using the built-in GUI system), probably some different car models, a way to store and specify the tunings, etc., etc. Probably best to break it down, IMO. Choose a starting point, and then post back with more specific questions if/when you have them.
     
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    How would I store different tunings for each car? :confused:
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    That's up to you, but if you want the settings to persist between runs of the application, you might take a look at PlayerPrefs.
     
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    What do you mean? I am new to making multiple things in games. ;) So, i wouldnt know how to store different tunings for each car or anything. :(
     
  8. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Well, you kind of have to work up to it :) Once you've programmed a bit and gained some experience, stuff like this will start to seem less mysterious.

    But, here's a place you can start. Create a script, and make the various tuning parameters public variables of that script. Then, create a game object for each type of car, and for each of them, set the tuning parameters to whatever you want them to be.

    Then, create a prefab from each game object (you can get rid of the game objects after that if you want - they were just for creating the prefabs). You can then create as many cars as you want of whatever type by instantiating clones from the prefabs. (What I'm describing here is basically the fundamentals of how Unity works, so if this isn't making sense, you'll want to spend some more time with the basics before trying to do something like what you describe.)

    If you want the player to be able to pick a car, you'll probably need some sort of menu or selection screen. You can represent the different cars by creating game objects from your prefabs and presenting them to the player (e.g. show them onscreen in a row or grid or something like that). You can then use picking in one form or another to determine which one the player has chosen. Then, when the game starts, create the player's car from whatever prefab they picked.

    If you want the player to be able to set the parameters themselves and have the parameters persist, then you get into PlayerPrefs and so on.

    To be honest, there's quite a bit to a customizable character/vehicle/whatever selection system like this, so it might not be the best starting point if you're completely new to programming. But, the above tips should at least get you started.
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I am using the car tutorial's script. I am already using it for another car. But, when I try to add it, it says that it already has the definition for the 'wheel'. :confused: Am I doing anything wrong?
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Well, probably, if it's resulting in an error :)

    I'm not familiar with that tutorial though, and I can't really say for sure what the problem is. A couple of pieces of information that would be useful though would be:

    1. What you mean by 'try to add it'. (Are you talking about adding a script to a game object? Code to a script? Something else?)

    2. The exact error message (you can just copy it and paste it into a post).
     
  11. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    1. Adding a script to a game object. I just copied and pasted the code into a new script for another car.

    2.
    Code (csharp):
    1. Assets/vwbeetletuning.js(56,7): BCE0132: The namespace '' already contains a definition for 'Wheel'.
    ;)
     
  12. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I can only guess at this point, but it sounds like you're trying to create multiple objects by copy-pasting a script multiple times? (Or something?)

    If so, that's not how you do it. Each script/class should only exist once. You can then create instances of that script/class in code or by dragging the script onto a game object, etc. Copying and pasting should not be involved.

    Again though, I'm just guessing, as I still don't know exactly what it is you're doing.
     
  13. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    That is what I'm doing(the copy-paste script multiple times.) I am doing that because I want each car to have different tuning. Ok. I will only have 1 script(And modify differently for each car). Will that work? Or, will it still be the same for all cars?
     
  14. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Ok. I made a prefab. A selection screen. And a button. It is supposed to load the level and Prefab. But, the button wont load the level. :confused:

    Code (csharp):
    1. var customSkin : GUISkin;
    2.  
    3. function OnGUI () {
    4.          GUI.skin = customSkin;
    5.          
    6.        //Make a button. If it is pressed, it will load "Illegal Racing" level1 . :)
    7.          if (GUI.Button (Rect (50, 200, 240, 70), "Load This Car!")) {
    8.                  var instance : GameObject = Instantiate(Resources.Load("CarBeetle"));
    9.                  Application.LoadLevel ("IllegalRacing");
    10.                  }
    11.                  
    12. }
    Any ideas why? Is my script okay?
     
  15. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
  16. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Not sure about the Resources.Load() part, but for loading the level, make sure the scene in question has been added to your build settings.

    Also, you might add some error-checking to make sure the object is being instantiated correctly. (Also, keep in mind that without taking additional steps that object will be destroyed almost immediately, since you're loading a new level.)
     
  17. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Is there a better way to instantiate that object when choosing it? :confused:
     
  18. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    To save time, PlayerPrefs can be slower when you save more variables. To fix this, have a prefab store those variables, then have your cars draw the variables out upon instantiation. Whenever the game closes or starts is when PlayerPrefs operations will take place.
     
  19. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    What are PlayerPrefs? I looked at the scripting guide, but was confused when I got to them. :confused:
     
  20. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    PlayerPrefs allows you to save and retrieve data that persists between runs of the application. (That is, it's saved on the hard drive somewhere so that it's still there the next time your game is launched). It's generally used for stuff like (naturally) player preferences (e.g. video and audio settings and so on), but can also be used for anything else you might need it for (within reason), such as saving a 'player configuration', or even saving an entire game state if it doesn't require too much data.

    Basically, PlayerPrefs allows you to save and retrieve strings, integers, and floats; what you do with that functionality is up to you.
     
  21. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Can anyone make me a base to work off of? That would definitely help me. :) Since, I am not the best at js. ;)
     
  22. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You'll probably just have to dive in and start learning how to program. What I'd recommend is to pick one aspect of the problem - something relatively simple - and tackle that first. If you run into problems, post here (or start a new thread) and describe exactly what you're trying to do and exactly what problems you're having, and someone should be able to help.
     
  23. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Well, I do not know where to start on this problem. Where would I start?
     
  24. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    First, define what it is exactly that you want to do to. In other words, describe (in writing, if it would help) exactly what you want to have happen. Don't worry about whether you know how to implement it at this point; just describe what, ideally, you'd like to have happen.

    Once you've done that, pick a single aspect of what you've described; it can be something completely trivial, like displaying a model onscreen. Or, it can be something a little more complex, like determining when an object has been clicked on. The important part is to choose one thing and one thing only, and to choose something that's fairly simple.

    Then, create a scene as a testbed in which to test that functionality, and only that functionality. Don't move on until you've figured it out and have some code that works. Once you've done that, move on to some other aspect of the problem. Repeat this process until you've accumulated bits of code that will do the different things you want. The last step is to integrate them, which isn't necessarily trivial, but will be impossible if you haven't solved the various sub-problems involved to begin with.

    So in summary, pick one aspect of the problem, and work on that first. If you get stuck, post back here or create a new thread specifically on that topic.
     
  25. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I've looked everywhere, but cannot find how to make a prefab model go into a GUI once the player has pressed a button. :confused:
     
  26. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You're not defining the problem in the right way. 'Make a prefab model go into a GUI' doesn't really make sense as stated, and if you can't state clearly what the implementation needs to do, you're going to have a hard time creating said implementation.

    Instead of talking about 'prefabs' and 'GUIs' at this point, I suggest (as I did earlier) that you start by describing the behavior that you want. Something like:

    I want three 3-d car models in a row in the center of the screen, each of which is slowly rotating about the up axis. At any given time, one of them is highlighted to indicate that it's selected. When the user clicks on one of the cars, if it's not already selected, it becomes selected. In addition to the cars is a button labeled 'Play'. When the user clicks this button, a new level is loaded. In this level, I want to instantiate a 'player' game object, but I want to use the model or prefab corresponding to the car that was selected in the previous scene.

    Once you have a description of the behavior you want, then you can start to break it down, as I described previously. The menu I described above probably involves solving 10 or 20 (or 30) different problems, assuming you want it to be totally polished and user-friendly. You can't solve all those problems at once; you have to solve them one at a time.

    So, you'd start by picking one aspect of it. For example, you could start by simply getting three car models lined up in a row, which would just involve creating the corresponding game objects and setting up the camera appropriately.

    A good next step would probably be to make the cars rotate. So, you tackle that problem next. And so on.

    In summary, stop thinking about prefabs and GUIs and trying to make it all happen at once. Just pick one aspect of the problem, and start with that.

    If you're still not sure where to start, describe the behavior you want clearly, and in as much detail as in my example above. Then, we can probably help point you towards a good starting place.
     
  27. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hello. i have created a model, and put it onto a screen. Where do I go from here?

     
  28. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Nice :)

    Here's two suggestions:

    1. If you want the car to rotate or do something else dynamic, you could tackle that next.

    2. Or, you could move on to user selection next. To begin with, you could just print a message to the console whenever the user clicks on the car.

    Either of those would be a good next step, I think.
     
  29. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I don't wanna do the rotation, btw. ;) But, what do you mean by the number 2 one?
     
  30. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, no rotation :)

    As for the second question, if you're not sure what that means, maybe you don't need it. So instead of us guessing, maybe you could just describe what you want your menu to do. In other words, what should it do that it doesn't do already? The answer to that question should provide some hints as to where to go next.
     
  31. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    All I want it to do is when I click the model, it'll instantiate that car I clicked to drive in my levels. ;)
     
  32. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, that's *almost* enough information :)

    The only part that's still unclear is that at this point it seems like there's only one car to choose from. If there's only one type of car, then instantiating that car in your levels is trivial; just instantiate or place the prefab as you would normally.

    It only becomes complicated if the player is allowed to choose from multiple cars. Is that what you're wanting to do? Or is it really something else that you're asking about?
     
  33. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    YES! Thanks.:) That is exactly what I want to do. ;)
     
  34. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, then the next thing I would do is get all the cars on the screen the way you want them. (That may seem like a small step, but you'll need to do that before you can move on to the next part.)
     
  35. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hello, I have created another car(It is obviously unfinished).



    Where do I go from here?
     
  36. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, next you want to determine when the user has clicked on one of the cars.

    The easiest way to do this is to use the built-in physics and collision detection system. Give each car an appropriately-shaped collider, and then use either the OnMouseDown() callback or the Physics.Raycast() function to determine when a car has been clicked on. For now, just print a message to the console to indicate this.

    OnMouseDown() is probably easier than Raycast(), so I'd go with that. The steps are then:

    1. Give your cars colliders.
    2. Create a script with an OnMouseDown() function and attach it to each of the 'car' game objects.
    3. In OnMouseDown(), print a message to the console, e.g. "I was clicked".

    Clicking on either car will print the same message, but that's ok for now.
     
  37. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I did that. But, it says for my script that the reference behavior is missing. :confused:
    My script is:
    Code (csharp):
    1.  
    2.  
    3. function OnMouseDown () {
    4. Debug.Log ("You clicked this car");
    5. }
    6.  
     
  38. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    No more help with my script? :(
     
  39. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Easy - it's only been two hours since your last post! :) I'm happy to help walk you through this, but keep in mind that even regular contributors don't generally monitor the forums 24/7. (Plus, the 'forum reply notification gnome' appears to be taking the weekends off these days.)

    'The referenced behavior is missing' usually means that the script in question has been removed from the project, I think. I'm not sure why you're getting that message in this case, but try removing the component that's generating that message (using the little 'gear' menu), and then dragging your 'on mouse down' script back onto the object. (Or, you can select the script from the drop-down menu for that component in the inspector.) If that's too involved, maybe just scrap the game object and build it again from scratch.

    If that doesn't fix it, maybe you could post a screenshot of your setup that shows the inspector for the object (and the error message).

    I don't use UnityScript, but your script seems ok, and I can't think of any reason you wouldn't be able to attach it to a game object. So, I'm guessing you've got something set up wrong somewhere.
     
  40. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    It doesn't print anything to the console when clicked. :( The collider(named 'collider'), is attached to the car gameObject. The collider is set to convex(I tried it with on and off). And, the script is attached to the car gameObject. Any ideas why it doesn't work?
     
  41. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    News: I got something. Instead of printing something, I just made it load a level, and it works when I click the cars!! :D Anyways, where do I go from here?
     
  42. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    If loading the level works, then it seems printing to the console should work as well...

    In any case, do you then want a prefab corresponding to the car that was clicked to be spawned in the new level? Is that the behavior you're after?
     
  43. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Yes. That's what I want it to do. :)
     
  44. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, that means that somehow or other you need to store ('remember') which car was picked, and then instantiate that car in the next level.

    This is probably the trickiest part of the process so far, so I'd recommend starting with the simplest possible solution. There's a few different ways you can make data 'persist' between levels, but an easy way is to use static variables. (Not sure exactly what the syntax for that is in UnityScript - maybe someone else can fill in that detail.)

    In some script or other (doesn't really matter which one - just do whatever makes sense to you) you'll need a public static variable that represents which car was selected. I think you can probably just make this variable of type GameObject and assign a prefab to it directly when a car is selected.

    Then, when the next level starts, when you instantiate the player's car, use the prefab stored in the static variable. (I haven't tested this, but I'm pretty sure it'll work.)

    You'll also need a way to determine what prefab should be selected when a car is clicked on. One way would be to give the 'selection menu' car script a public GameObject variable, assign an appropriate prefab to each car on the menu screen, and then assign that prefab to the static variable when the car is clicked on.

    If that doesn't make sense or give you enough to go on, then (as always) try to break it down and identify the specific aspects of it that you're unsure of.
     
  45. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I'm working on that part. Here is the script attached to the car in the selection screen:

    Code (csharp):
    1. public var MP4 : GameObject;
    2.  
    3. function OnMouseUp () {
    4. transform.Find("GameObject");
    5. }
    Where should I go on making a script to remember that?
     
  46. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Just a bump. ;)
     
  47. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    In your current code, this line:

    Code (csharp):
    1. transform.Find("GameObject");
    Doesn't do anything (I'm not sure what you intend for it to do, but it's basically a no-op).

    As for your question, you'll probably want to add a public static variable of type GameObject to the script, and then in OnMouseUp(), assign MP4 to the static variable. Then, when the next level starts, the static variable should point to the prefab that you want to instantiate. (Assuming everything else has been set up correctly.)
     
  48. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    My friend showed me this:
    Code (csharp):
    1. function Awake () {
    2. DontDestroyOnLoad (transform.gameObject);
    3. }
    Would that work? :D If so, How would I get the car?
     
  49. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Yes, that's one of several ways to cause data to persist between levels.

    To get the car prefab, you'd need to assign it to a variable associated with the object marked as 'don't destroy on load', and then retrieve the value when the new level is loaded (which would most likely involve finding said object by e.g. name or tag).

    The reason I recommended the static variable method is that it's a little more straightforward to implement. But, using a persistent object will work too.
     
  50. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Can you give me an example script for your idea?