Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Show hide swap groups

Discussion in 'Scripting' started by natmaxex, Oct 14, 2021.

  1. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I'm making a multiplayer game where players have 3 "layers"(groups of GameObjects) they can place and swap between by turning the objects in each group on and off.
    I don't know the best way to do that.
    i was originally going to have players spawn the game objects and name them
    Code (CSharp):
    1. //Name = player Name
    2. //ID = counting upwards each time spawned on the current layer
    3. //layer = what layer is set as visible when spawned
    4. "objName, name, iD, 0, layer, 0,"
    and use
    Code (CSharp):
    1. //find all objects related to the layer and make them visible while hiding everything else.
    2. GameObject.Find();
    But I was told in this post that that is a bad idea. Here
     
    Last edited: Oct 14, 2021
  2. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I think i figured out what i need to do.
    I should probably make lists or arrays inside the player that contains the values.
    I would like to know how to use player prefs, XML, or jquery to save the player information for later. especially since players might want to come back and make changes to their layers
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You really could just use the above in a Dictionary<string,GameObject> and manage the list of things yourself.

    I think you need to expand more on what this means exactly:

    and by "expand" I mean how are these collections manipulated, what are the requirements for addressing these things. Individual? Or as these three separate large groups all at once? etc.
     
    natmaxex likes this.
  4. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    What if a player spawns 7 GameObjects and the player needs to remove GameObject 3 won't that cause errors and how do I fix that.

    Disclamer: Code is an example and will be full of mistakes.

    Code (CSharp):
    1. //number of layers can be changed
    2. //how do i make Layers changeable
    3.  
    4. //this holds all the spawned gameobjects
    5. GameObject[] spawnedGameobjects;
    6. string[] playerNames;//maby 2 or 3 player for this example
    7. //this tells how may gameObjects have been spawned in total
    8. int numberOfSpawnedGameobjectsTotal,// not sure if this is necessary
    9. int[]
    10. layerID,//for this example there are 3 layers
    11. layer0,//there are 2 gameobjects in this layer
    12. layer1//there are 3 gameobjects in this layer
    13. layer2;//there are 2 gameobjects in this layer
    14.  
    15. //true = spawn
    16. //false = remove
    17. bool spawnRemove = true;
    18.  
    19. GameObject prefabClone;
    20.  
    21. void Update()
    22. {
    23.     if (Input.GetButtonDown("Fire1")) //Fire1 is mouse 1st click
    24.     {
    25.      
    26.      
    27.         if(spawnRemove){
    28.             //mouseActionID = 0;
    29.             CmdShootRay(0);
    30.         }else{
    31.             //Destroy gameobject mouse is hovering over
    32.             //or shold i create a bool variable for visavbility (that dosnt seam like its good for performance)
    33.         }
    34.     }
    35. }
    36.  
    37.  
    38. [Command]
    39. void CmdShootRay(int CmdDrawActionID)
    40. {
    41.     RpcFireWeapon(CmdDrawActionID);
    42. }
    43.  
    44. [ClientRpc]
    45. void RpcFireWeapon(int RpcDrawActionID)
    46. {
    47.     prefabClone = Instantiate(netMan.spawnPrefabs.Find(prefab => prefab.name == "gameObjClone"));
    48.  
    49.     prefabClone.transform.position = transform.position;//this object is mouse position
    50.  
    51.     prefabClone.name = "" + playerNames[numberOfSpawnedGameobjectsTotal];
    52.  
    53.     numberOfSpawnedGameobjectsTotal += 1
    54.  
    55.     if(layerID == 0){
    56.         layer0 += 1;
    57.     }
    58.  
    59.     if(layerID == 1){
    60.         layer1 += 1;
    61.     }
    62.  
    63.     if(layerID == 2){
    64.         layer2 += 1
    65.     }
    66.  
    67.     spawnedGameobjects.Add = prefabClone;
    68. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I think you are confusing "fix" for proper engineering.

    It's your game design.

    We know computers can keep track of anything you want.

    Bring those things together.
     
  6. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    Yes, I am not using proper engineering, but I would like to.
    I will continue to lean and work towards that end.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    One great way to expand your brain in this regard is to genuinely work through tutorials for games that contain specific features that you imagine for your game, even if they're not the same game.

    This is what I mean by "genuinely work through:"

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  8. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I'm dont doing tutorials. im not sure why you are working so hard to torment me. If you don't know that's fine theres no need for your insults.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Don't read my mind because you're not good at it. I'm actually trying to help you.

    Good luck.
     
  10. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    So you dont know.
     
  11. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Dude, no one will help you with this attitude.

    Try to be concise when you describe your problem, describe it in full before you present what you think would be the closest to a solution, but doesn't work for some reason.

    We are here to help you out when you have some problem and you actually ran out of ideas, not brainstorming what would happen if you do X. Just do X and see it for yourself and if you can't find a solution come back, describe the situation, list what did you try and see if anyone has any ideas.

    But things like
    don't give so much confidence that you are actually ready for what you're doing.
    Be prepared that if we think you aren't ready what you're doing we won't really teach you, we send you away recommending some tutorials and exercises so you can learn and master your craft and come back when you actually know what you're doing and more importantly why you're doing it.
     
  12. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
    I'll work on it.
    Why am I being harassed and attacked what did I do?
     
    Last edited: Oct 15, 2021