Author Topic: Instantiate object !  (Read 1745 times)

biyarjomandi

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 11
    • View Profile
Instantiate object !
« on: July 06, 2016, 06:09:24 AM »
hi .
I don't know why this code instantiate more than one object ? is there any problem with this ?
  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4. using TNet;
  5.  
  6.         public class PlayBTNque : MonoBehaviour
  7.         {
  8.  
  9.                 /// <summary>
  10.                 /// Prefab to instantiate.
  11.                 /// </summary>
  12.  
  13.                 public string prefabPath;
  14.  
  15.                 /// <summary>
  16.                 /// Whether the instantiated object will remain in the game when the player that created it leaves.
  17.                 /// Set this to 'false' for the player's avatar.
  18.                 /// </summary>
  19.  
  20.                 public bool persistent = false;
  21.  
  22.         IEnumerator Start ()
  23.         {
  24.                 TNManager.JoinChannel (2, true, true);
  25.                 while (TNManager.isJoiningChannel) yield return null;
  26.  
  27.                 TNManager.Instantiate (2, "CreateAtPosition", prefabPath, persistent, transform.position, transform.rotation);
  28.                 Debug.Log ("Object created");
  29.                 Destroy (gameObject);
  30.                                
  31.                                         }
  32.  
  33.                 [RCC]
  34.                 static GameObject CreateAtPosition (GameObject prefab, Vector3 pos, Quaternion rot)
  35.                 {
  36.                         // Instantiate the prefab
  37.                         GameObject go = prefab.Instantiate();
  38.  
  39.                         // Set the position and rotation based on the passed values
  40.                         Transform t = go.transform;
  41.                         t.position = pos;
  42.                         t.rotation = rot;
  43.                         return go;
  44.                 }
  45.         }
  46.  
  47.  

Debug.Log ("Object created") prints in console just one time but more than one network object instantiates !.
« Last Edit: July 06, 2016, 06:15:37 AM by biyarjomandi »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instantiate object !
« Reply #1 on: July 06, 2016, 07:11:18 PM »
Assuming the "persistent" flag remains as 'false', the objects you create will be destroyed when you leave. However if at any point you had it as 'true', that object will remain in the channel as you're joining it passing 'true' in the TNManager.JoinChannel request -- meaning all objects created with the 'persistent' flag being true effectively stick around.