Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple cars

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

  1. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Maybe someone will post a complete example. In the meantime though, working through some more tutorials and learning a little more about scripting will probably help. These sorts of things can seem quite mysterious when you've just started learning how to program, but once you gain a little knowledge and experience, it will start to become obvious how to solve problems like this :)
     
  2. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Okay. What do you mean by 'load a static variable' ?
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Hm, I don't think I said anything about 'loading' a static variable. What you would do though is create a static variable, assign the prefab you want to use to that variable when a car is clicked on, and then retrieve the prefab from that variable when the new level starts.

    I'm sure the syntax in UnityScript is straightforward, but unfortunately I don't know it off the top of my head, so I'll have to let someone else provide an example.
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    ok. An example would be nice. I can learn from it. :)
     
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    BUMP. Sorry to bump an older thread. I still do not know where to go from here on this subject. :confused:
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Still working on this, eh? :)

    If you want to use the 'static variable' method, then the first thing you'll need is a static variable. I don't know the syntax for declaring a static or global variable in UnityScript off the top of my head, but it should be easy enough to find out. Try searching the forums using (e.g.) the following terms:

    'javascript static variable'
    'javascript global variable'
    'unityscript static variable'
    'unityscript global variable'

    And you should find some examples. If you can't find anything though, I'd just post a new thread, e.g. 'How to create a static/global variable in UnityScript?', and someone should answer.

    Once you've got that figured out, you can post back to this thread and pick up where you left off.
     
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Well, Seems as though you just pit 'static' in from of your variable.

    Code (csharp):
    1.  
    2. static var Car : Transform;
    3.  
    Now where should I go from here?
     
  8. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    When the player clicks on one of the cars in your 'car selection' menu, you need to assign a corresponding prefab to the 'Car' variable (the name of which should probably start with a lower-case letter, if only by convention).

    One way to do this would be to add a 'car prefab' variable to the script that's attached to the cars in the menu. Then, when a click is detected, assign that prefab to the static 'car' variable. That way, the static 'car' variable will always refer to the prefab corresponding to the last car that was clicked on.

    This part is indeed a little trickier than what's been involved so far. But, if you break it down logically, it should make sense. Here's what you know:

    1. There's a different prefab for each car the player can use
    2. Each 'car' object in the menu corresponds to one of these prefabs
    3. The static 'car' variable should always reference the prefab corresponding to the car that was clicked on last
    4. When you then start the level, the prefab stored in the static 'car' variable should be used to create the car instance for that level

    Given that information, you should be able to piece together a solution.
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    I thought of using Resources.Load to load a Prefab from the Resources folder. I couldn't find anything on referencing/assigning a prefab/car I clicked to a variable. So, I'm stuck here. :confused:
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Although you could solve this problem by loading resources dynamically, you shouldn't have to.

    You're probably not going find anything on that particular topic. That's not really how it works.

    Learning programming is kind of like learning a spoken language; you learn the words, grammar, and idioms, and then you can construct sentences on your own. A book on a spoken language isn't going to explain how to construct every possible sentence in that language separately. Similarly, the Unity references and tutorials aren't going to explain how to do every single possible thing you could ever want to do in Unity. In most cases, you'll have to figure it out yourself.

    So instead, you need to break what you want to do down into individual 'words' and 'phrases', and figure those out. You say you're not sure how to proceed, so let's just start with something simple: Do you know how to assign a value to a variable?
     
  11. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Yes.

    Code (csharp):
    1.  public var Number : float = 5;
    I can edit that number in the inspector. :)
     
    Last edited: Mar 7, 2011
  12. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Ok, that's good, but remember what we need here is a static variable (which can't be edited directly in the inspector).

    Ok, next, do you have a script that's attached to the 'car' game objects in your menu? (I'm assuming you do, since that script is what detects when the cars are clicked on.)

    Assuming you have such a script, the next step is to add a public (non-static) variable of type GameObject to that script (there are other types you could use, but I always use GameObject for prefabs). Since this variable will be public, it'll show up in the inspector and you'll be able to assign prefabs to it.

    The next thing you'll want to do after that is, for each 'car' game object in the menu scene, assign an appropriate prefab to this variable (where the prefab is whatever car you want the player to end up using if they click on that car).
     
  13. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Ok. So, I put both GameObject/Transform variables on the OnMousedown script for each car?

    Code (csharp):
    1.  
    2. public var MP4 : GameObject;
    3. public var Charger : GameObject;
    4.  
    Do that for both and assign?
     
  14. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Are 'MP4' and 'Charger' different kinds of cars? (I assume Charger is, but I'm not sure what MP4 refers to.)

    If those are the different kinds of cars you want the user to select from, then you don't want a variable for each car type. Rather, you want *one* variable that corresponds to the type of car associated with that menu item, e.g.:

    Code (csharp):
    1. var carType : GameObject;
    And then you assign the appropriate prefab to that variable.
     
  15. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Ok. So, the OnmouseDown script now looks like this.

    Code (csharp):
    1. public var MP4 : GameObject;
    2.  
    3. function OnMouseUp () {
    4.          Application.LoadLevel("IllegalRacing");
    5. }
    Each car has a different script(Charger instead of MP4). And the Prefabs are assigned to each accordingly. Now what?
     
  16. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I'm not sure whether what I'm trying to convey is getting across successfully. In any case, it might be time for you to go into 'problem-solving' mode :) You'll encounter many, many problems of just this sort in the process of creating a game, so at some point you'll have to get comfortable working through the logic yourself and devising appropriate solutions.

    Basically, if you understand what prefabs are, what they're for, and how they work, and if you understand that a static variable can be used to store data between scenes, then you can solve this problem. If you don't fully understand either of those things, then you'll want to work on some less complex projects first in order to get comfortable with the concepts. If you *do* understand both of those things, then you need to:

    - Assign an appropriate prefab to the static variable when a car is clicked in the menu.
    - Instantiate the player's car from that prefab when the new level starts.

    If you have specific questions or problems (e.g. 'here's my code, and here's the error I'm getting'), post them here, and someone will likely be able to help.
     
  17. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    The only thing I do not know how to do on this is add the GameObject to the static variable, and load that variable to an empty gameobject in the game when selected. :(
     
  18. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    To assign the game object to the static variable (all code untested):

    Code (csharp):
    1. myStaticVariable = myGameObjectVariable;
    To instantiate a game object from the game object stored in the static variable:
    Code (csharp):
    1. Instantiate(myStaticVariable, position, orientation);
     
  19. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Ok. Now, How would I assign that to my car(s)?