Author Topic: Instantiate object as child (not working)  (Read 2505 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Instantiate object as child (not working)
« on: October 31, 2016, 11:15:53 PM »
Im trying to instantiate an object as a child of another with tnet on a movable player, but it doesn't seem to be working.....

  1. public GameObject launchpoint; <------ this object is to be parent
  2.  
  3. public int channelID = 0;
  4.  
  5. public void T1A1press(){
  6.  
  7.                         if (channelID < 1) channelID = TNManager.lastChannelID;
  8.                 TNManager.Instantiate(channelID, "T1A1go", "Prefabs/Projectiles/t1a1", false, launchpoint.transform.position, launchpoint.transform.rotation);
  9.  
  10.         }
  11.         [RCC]
  12.         static GameObject T1A1go (GameObject prefab, Vector3 pos, Quaternion rot)
  13.         {
  14.                 // Instantiate the prefab
  15.                 GameObject go = prefab.Instantiate() as GameObject;
  16.  
  17.                 // Set the position and rotation based on the passed values
  18.                 Transform t = go.transform;
  19.                 t.position = pos;
  20.                 t.rotation = rot;
  21.  
  22.                 go.GetComponent<Rigidbody> ().AddRelativeForce (Vector3.forward * 1000.0f);
  23.  
  24.  
  25.  
  26.                 return go;
  27.         }
  28.  
  29.  


What happens is that during gameplay, the instantiated object spawns at original location of the player which has "launchpoint" as a child...but when my player moves, future objects spawn at the original location of the player, rather than the new location of the player...

Can anyone pick up what I am doing wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instantiate object as child (not working)
« Reply #1 on: November 03, 2016, 08:25:06 AM »
Child TNObjects have IDs of their parent and all communication goes through the parent. I actually expanded on this in the latest version of TNet on my end (not yet public), but it's still the same.

You can instantiate two objects then set one to be parented to another at run time yourself, just be aware that destroying the root object will end up destroying the local copy of the child, without actually removing it from the server -- likely leading to errors.

It's often better to just set one object to copy the transform of another in Update() or LateUpdate().

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Instantiate object as child (not working)
« Reply #2 on: November 04, 2016, 06:46:20 PM »
Thanks for responding Arenmook... Your response helped guide me into the right direction....