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 ChestHole · Feb 22, 2014 at 07:13 PM · networkingrpcnetwork.instantiatecardturnbased

Running only one script across clients and server

I am making a trading card game. I have a Game Controller script that fills up lists with game objects, and instantiates cards based on those lists. Both the card backs to click on that act as decks, and the cards themselves.

When I went to make my game networked, I ran into some trouble. I want to assign heirlooms to each player with the ComputerPickHeirlooms function, and then show them the heirlooms that were picked with the ShowHeirloomsToPlayer function.

I want the both players to pull out of the same deck, so there should be no repeats. Unless you are very lucky in my script, there are repeats.

I think what is happening is each computer is running a different instance of the script. How do I make it only make one instance that each player can look at? Alternately, how do I make it so they pull out of the same deck?

Here is GameController.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameControllerNetwork : MonoBehaviour {
 
     //These are public objects I have pulled into the GameController prefab's inspector.
     public static bool startBool = false;
     public GameObject cardback;
     public GameObject fivegold;
     public GameObject decoy;
     public GameObject fireskeleton;
     public GameObject flicker;
     public GameObject gremlins;
     public GameObject iceminotaur;
     public GameObject jelly;
     public GameObject venomviper;
     public GameObject coldroom;
     public GameObject cursedroom;
     public GameObject fireroom;
     public GameObject poisonroom;
     public GameObject stoneroom;
     public GameObject coldtrap;
     public GameObject cursedtrap;
     public GameObject firetrap;
     public GameObject poisontrap;
     public GameObject stonetrap;
     public GameObject familyjewel;
     public GameObject timestwo;
     public GameObject swordofwar;
     public GameObject hammeroflife;
     public GameObject necklaceofregeneration;
     public GameObject glovesofpilfering;
     public GameObject staffofdeath;
     public GameObject shieldofpeace;
     public GameObject unlocked;
     public GameObject fireknight;
     public GameObject frontlineminer;
     public GameObject frostmage;
     public GameObject lichmage;
     public GameObject ranger;
     public GameObject thief;
     
     public GameObject[] cardToDestroy;
     
     //GetClicked.cs variables 
     //GetClicked.cs says 
     //OnMouseDown, if (clickCounter == howManyClicks && ranFunction == false) 
     //SendMessage whatFunction; ranFunction = true; else clickCounter++;
     public static int clickCounter;
     public static string whatFunction;
     public static bool ranFunction = false;
 
     public static int howManyClicks;
     public static int cardsDealt;
     
     public List<GameObject> heirloomDeck = new List<GameObject>();
     public static List<GameObject> heirloomHandp1 = new List<GameObject>();
     public static List<GameObject> heirloomHandp2 = new List<GameObject>();
     public static List<GameObject> cardBackDeck = new List<GameObject>();
     
     // Use this for initialization
     void Start () {
     }
     
     void StartHeirlooms() {
         Debug.Log ("Start Heirlooms!");
         
         InitializeCardBackDeck ();
     }
     
     void InitializeCardBackDeck()
     {
         Debug.Log ("CardBackDeck Initialization");
         cardBackDeck.Add (cardback);
         cardBackDeck.Add (cardback);
         cardBackDeck.Add (cardback);
         cardBackDeck.Add (cardback);
         cardBackDeck.Add (cardback);
         cardBackDeck.Add (cardback);
         whatFunction = "ShowHeirloomsToPlayer";
         howManyClicks = 3;
         ShowCards (cardBackDeck.Count, cardBackDeck, 1.5f, 1.5f, "Cardbacks");
         InitializeHeirlooms();
     }
     
     void InitializeHeirlooms(){
         Debug.Log ("HeirloomDeck Initialization");
         heirloomDeck.Add(swordofwar);
         heirloomDeck.Add(hammeroflife);
         heirloomDeck.Add(necklaceofregeneration);
         heirloomDeck.Add(glovesofpilfering);
         heirloomDeck.Add(staffofdeath);
         heirloomDeck.Add(shieldofpeace);
         ComputerPickHeirlooms();
     }
     
     //Once deck Lists are filled up, pick three random Heirlooms and put them in a List for p1, then p2
     void ComputerPickHeirlooms()
     {
         Debug.Log ("Computer picks Heirlooms for p1");
         for (int i = 0; i < 3; i++) {
             int randomNumber = Random.Range (0, heirloomDeck.Count - 1);
             if (heirloomDeck[randomNumber] == swordofwar) { heirloomHandp1.Add(swordofwar); }
             else if (heirloomDeck[randomNumber] == hammeroflife) { heirloomHandp1.Add(hammeroflife); }
             else if (heirloomDeck[randomNumber] == necklaceofregeneration) { heirloomHandp1.Add(necklaceofregeneration); }
             else if (heirloomDeck[randomNumber] == glovesofpilfering) { heirloomHandp1.Add(glovesofpilfering); }
             else if (heirloomDeck[randomNumber] == staffofdeath) { heirloomHandp1.Add(staffofdeath); }
             else if (heirloomDeck[randomNumber] == shieldofpeace) { heirloomHandp1.Add(shieldofpeace); }
             else { Debug.Log ("Something has gone terribly wrong."); }
             heirloomDeck.RemoveAt(randomNumber);
             Debug.Log (heirloomHandp1[i] + " Heirloom card for p1");
         }
         
         Debug.Log ("Computer picks Heirlooms for p2.");
         for (int i = 0; i < 3; i++) {
             int randomNumber2 = Random.Range (0, heirloomDeck.Count - 1);
             if (heirloomDeck[randomNumber2] == swordofwar) { heirloomHandp2.Add(swordofwar); }
             else if (heirloomDeck[randomNumber2] == hammeroflife) { heirloomHandp2.Add(hammeroflife); }
             else if (heirloomDeck[randomNumber2] == necklaceofregeneration) { heirloomHandp2.Add(necklaceofregeneration); }
             else if (heirloomDeck[randomNumber2] == glovesofpilfering) { heirloomHandp2.Add(glovesofpilfering); }
             else if (heirloomDeck[randomNumber2] == staffofdeath) { heirloomHandp2.Add(staffofdeath); }
             else if (heirloomDeck[randomNumber2] == shieldofpeace) { heirloomHandp2.Add(shieldofpeace); }
             else { Debug.Log ("Something has gone terribly wrong."); }
             heirloomDeck.RemoveAt(randomNumber2);
             Debug.Log (heirloomHandp2[i] + " Heirloom card for p2");
         }
     }
     
     void ShowHeirloomsToPlayer()
     {
         //shows Heirlooms to player, quit changing
         DestroyHand ("Cardbacks");
         networkView.RPC("ShowHeirloomCardsRPC", RPCMode.All);
     }
     
     //Destroys cards with tagname
     void DestroyHand(string tagname)
     {
         cardToDestroy = GameObject.FindGameObjectsWithTag(tagname);
         foreach (GameObject move in cardToDestroy) {
             move.AddComponent("Die");
         }
     }
     
     // Shows heirloomHandp1 to p1 who is server, shows heirloomHandp2 to p2 who is client.p'
     [RPC] void ShowHeirloomCardsRPC ()
     {
         if (networkView.isMine){
             Debug.Log("Showing" + heirloomHandp1.Count + "  Heirlooms to server.");
             //ShowCardsToNetwork (heirloomHandp1.Count, heirloomHandp1, -6.5f, 1.5f, "ShownHeirloomCards");
             ShowCards (heirloomHandp1.Count, heirloomHandp1, -6.5f, 1.5f, "ShownHeirloomCards");
         }
         if(!networkView.isMine){
             Debug.Log("Showing" + heirloomHandp2.Count + "  Heirlooms to others.");
             //ShowCardsToNetwork (heirloomHandp2.Count, heirloomHandp2, -8.5f, 1.5f, "ShownHeirloomCards");
             ShowCards (heirloomHandp2.Count, heirloomHandp2, -8.5f, 1.5f, "ShownHeirloomCards");
         }
         
     }
     
     void ShowCardsToNetwork(int handCount, List<GameObject> deck, float targetx, float targety, string tagname)
     {
         cardsDealt = 0;
         for (int i = 0; i < handCount; i++)
         {
             Debug.Log("Showing " + deck[cardsDealt]);
             DealCardToNetwork (deck, cardsDealt, targetx, targety, tagname);
             cardsDealt++;
         }
     }
     
     public GameObject DealCardToNetwork (List<GameObject> deck, int cardsDealt, float targetx, float targety, string tagname)
     {
         if (deck[cardsDealt] != null)
         {
             Debug.Log("Showing " + deck[cardsDealt]);
             Vector2 targetxy = new Vector2(targetx, targety);
             GameObject go = Network.Instantiate (deck [cardsDealt], targetxy, Quaternion.identity, 0) as GameObject;
             go.transform.position = new Vector2 ((float)targetx + cardsDealt / 1.5f, (float)targety + cardsDealt / 1.5f); // place card 1/4 up on all axis from last
             if (howManyClicks != 0) {
                 Debug.Log("Putting getclicked and boxcollider on cards because howManyClicks = " + howManyClicks);
                 go.AddComponent ("GetClicked");
                 go.AddComponent ("BoxCollider");
                 go.collider.isTrigger = true;
                 go.tag = tagname;
             }
             else {
                 Debug.Log("Didn't put getclicked on card because howManyClicks = 0");
             }
             return go;
         }
         else { Debug.Log("Put cards in " + deck); }
         return null;        
     }
     
     void ShowCards(int handCount, List<GameObject> deck, float targetx, float targety, string tagname)
     {
         cardsDealt = 0;
         for (int i = 0; i < handCount; i++)
         {
             Debug.Log("Showing " + deck[cardsDealt]);
             DealCard (deck, cardsDealt, targetx, targety, tagname);
             cardsDealt++;
         }
     }
     
     public GameObject DealCard (List<GameObject> deck, int cardsDealt, float targetx, float targety, string tagname)
     {
         if (deck[cardsDealt] != null)
         {
             Debug.Log("Showing " + deck[cardsDealt]);
             Vector2 targetxy = new Vector2(targetx, targety);
             GameObject go = GameObject.Instantiate (deck [cardsDealt]) as GameObject;
             go.transform.position = new Vector2 ((float)targetx + cardsDealt / 1.5f, (float)targety + cardsDealt / 1.5f); // place card 1/4 up on all axis from last
             if (howManyClicks != 0) {
                 Debug.Log("Putting getclicked and boxcollider on cards because howManyClicks = " + howManyClicks);
                 go.AddComponent ("GetClicked");
                 go.AddComponent ("BoxCollider");
                 go.collider.isTrigger = true;
                 go.tag = tagname;
             }
             else {
                 Debug.Log("Didn't put getclicked on card because howManyClicks = 0");
             }
             return go;
         }
         else { Debug.Log("Put cards in " + deck); }
         return null;        
     }
     
     
     
     // Update is called once per frame
     void Update () {
     //if (GameControllerNetwork.startBool == false && NetworkManager.Connected) {
     //    StartHeirlooms();
     //    GameControllerNetwork.startBool = true; }
     }
 }

And here is the key function in my NetworkController.cs

 void OnConnectedToServer()
     {
         Debug.Log("Server Joined");
         connected = true;
         Network.Instantiate(gameControllerPrefab, transform.position, Quaternion.identity, 0);
     }
 
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

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

RPCs and instantiations being dropped/appearing out of order 1 Answer

Changing a variable in another script over the network 1 Answer

RPC and how to talk to clients 0 Answers

UNET Networking: where and when to initialize instantiated player on the client? 0 Answers

Send a Pause RPC on OnApplicationPause 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