Search Unity

Create gameobject in certain Canvas

Discussion in 'UGUI & TextMesh Pro' started by nikolas888, Sep 30, 2020.

  1. nikolas888

    nikolas888

    Joined:
    Dec 21, 2019
    Posts:
    5
    Hi everyone,
    I made to develop a piece of code where i try to put group of gameobject and show them into canvas.
    The code is looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using Photon.Pun;
    4. using Assets.Scripts.UI.Game.CheckCards;
    5. public class BeginningOfGame : MonoBehaviourPun
    6. {
    7.  
    8.     [SerializeField]
    9.     private Canvas canvacesOfFirstDeck;
    10.     private ZingDealer _zingDealer;
    11.  
    12.     [SerializeField]
    13.     private Canvas canvacesOfCurrentPlayer;
    14.  
    15.     [SerializeField]
    16.     private Text _textFirstPlayer;
    17.  
    18.  
    19.     [SerializeField]
    20.     private Text _textSecondPlayer;
    21.  
    22.  
    23.     private void Awake()
    24.     {
    25.         if (PhotonNetwork.IsMasterClient)
    26.         {
    27.             _zingDealer = new ZingDealer();
    28.  
    29.         }
    30.  
    31.     }
    32.     void Start()
    33.     {
    34.         string name = GameManagerSingleton.Instance.GetPlayerName();
    35.         _textFirstPlayer.text = name;
    36.  
    37.         sendDataAboutUser();
    38.  
    39.         InitBoard();
    40.         InitTalonCards();
    41.  
    42.     }
    43.  
    44.     private void InitBoard()
    45.     {
    46.  
    47.     }
    48.  
    49.     private void sendDataAboutUser()
    50.     {
    51.         var PlayerName = GameManagerSingleton.Instance.GetPlayerName();
    52.  
    53.         // object[] data = new object[] { PlayerName };
    54.         _textSecondPlayer.text = PlayerName == PhotonNetwork.CurrentRoom.GetPlayer(2).NickName
    55.            ? PhotonNetwork.CurrentRoom.GetPlayer(1).NickName
    56.            : PhotonNetwork.CurrentRoom.GetPlayer(2).NickName;
    57.  
    58.     }
    59.  
    60.    
    61.  
    62.     private void InitTalonCards()
    63.     {
    64.         float startPosition = 2.0f;
    65.         float multiplier = 1.15f;
    66.  
    67.  
    68.         foreach (var obj in _zingDealer.TalonCards)
    69.         {
    70.             GameObject gameObj = (GameObject)obj;
    71.             // Preparation to init is next
    72.             // Check later to scale svg. Probably we will have better sharpness.
    73.             gameObj.transform.localScale = new Vector3(0.3f, 0.3f);
    74.             gameObj.transform.localPosition = new Vector3(startPosition * multiplier - 6.80f, 2.5f, -1.0f);
    75.  
    76.  
    77.             GameObject firstDeck = (GameObject)Instantiate(gameObj, new Vector3(0, 0, 0), Quaternion.identity);
    78.             firstDeck.transform.SetParent(canvacesOfFirstDeck.transform, false);
    79.             multiplier += 1.15f;
    80.          
    81.             // Instantiate(firstDeck);
    82.          
    83.         }
    84.     }
    85. }

    At this point on run i can see in palliate the object are created and have proper values but the problem is on GUI it is not showing(attached picture).

    When i comment the code where i trying to put gameobjects in canvas and just to show them using the method Instantiate...It is showing on GUI but all of gameobjects are created globally and they not in certain canvas.


    Thanks,
    Nikola
     

    Attached Files:

  2. nikolas888

    nikolas888

    Joined:
    Dec 21, 2019
    Posts:
    5
    I figure out where was a problem.
    Code (CSharp):
    1.  //gameObj.transform.localPosition = new Vector3(startPosition * multiplier - 6.80f, 2.5f, -1.0f);
    2.  
    3.  
    4.             GameObject firstDeck = (GameObject)Instantiate(gameObj, new Vector3(startPosition * multiplier - 6.80f, 2.5f, -1.0f), Quaternion.identity);
    the problem was in settings of local position.