Search Unity

Issue with Instantiate position

Discussion in '2D' started by PeaceLaced, Jul 21, 2016.

  1. PeaceLaced

    PeaceLaced

    Joined:
    Jun 2, 2015
    Posts:
    53
    Hi All,

    I am having an issue Instantiating an object in relation to another object. Both VS Debug and Debug.Log show the correct coordinates, yet the object does not spawn with these coordinates.

    >> Below shows the SunPosition as Vector3 (203.0 , 286.0 , 0.0)
    MSVS-Debug.png

    >> Below shows the PlanetPosition as Vector3 (205.0 , 286.0 , 0.0)
    MSVS-Debug2.png

    >> And both of them ready to print to Debug.Log
    MSVS-Debug3.png

    >> At the bottom you can see the Debug.Log with both the Vector3
    >> First clone selected has Transform that is correct.

    Unity-sys1.png

    >> Second Clone shows Transform Position that is incorrect.
    Unity-sys2.png

    Any help would be greatly appreciated.
    ~ PeaceLaced
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Are both planets being spawned with that snippet in the screenshot? Is that within a loop or something?
     
  3. PeaceLaced

    PeaceLaced

    Joined:
    Jun 2, 2015
    Posts:
    53
    This is the Method I am building. The PlanetPosition is used the same as the SunPosition, with instantiate. Sun works, Planet does not.

    Code (CSharp):
    1.  
    2.     public void SpwanTheSystem()
    3.     {
    4.         //---------------------------------------------------------------
    5.         //---------------------------------------------------------------
    6.         //---------------------------------------------------------------
    7.         // Randomly generate the sun masses and Randomly disperse them on the play field
    8.         //--------------------------------------------------------------
    9.         //--------------------------------------------------------------
    10.         //--------------------------------------------------------------
    11.  
    12.         // Add SuperMassiveMass to the full mass.
    13.         FullMass = FullMass + SuperMassiveMass;
    14.  
    15.         // Create the list object that holds float numbers
    16.         List<float> celestialMasses = new List<float>();
    17.  
    18.         int SunCount = CelestialBodySuns;
    19.         // While we still have suns to generate mass for
    20.         while (SunCount > 0)
    21.         {
    22.             // Randomly generate masses between 100,000 and 500,000
    23.             float SunMass = Mathf.Floor(Random.Range(100000f, 500000f));
    24.  
    25.             // Add them to the celestialMasses List
    26.             celestialMasses.Add(SunMass);
    27.  
    28.             // Add them to the Full Mass counter
    29.             FullMass = FullMass + SunMass;
    30.  
    31.             // Create a new Vector3 that will place the object with Random X and Y coordinates
    32.             //Vector3 SunPosition = new Vector3(SunCount * 2, SunCount * 2, 0);
    33.             Vector3 SunPosition = new Vector3(Mathf.Floor(Random.Range(-100 , 100)), Mathf.Floor(Random.Range(-100 , 100)),0);
    34.  
    35.             // Create a new gameobject that will instantiate the celestialbodyprefab at the above vector3 position
    36.             GameObject SunObject = Instantiate(CelestialBodiesPrefab, SunPosition, Quaternion.identity) as GameObject;
    37.  
    38.             // Access its RigidBody2D compoenent and modify it to our liking
    39.             CelestialBodyRigidBody2D = SunObject.GetComponent<Rigidbody2D>();
    40.             CelestialBodyRigidBody2D.mass = SunMass;
    41.             CelestialBodyRigidBody2D.drag = 10; //ToDO: possibly modify this based on Mass???
    42.  
    43.             // Access its CircleCollider2D component and modify it to our liking
    44.             CelestialBodyCircleCollider2D = SunObject.GetComponent<CircleCollider2D>();
    45.             CelestialBodyCircleCollider2D.radius = SunMass/40000;
    46.  
    47.             // Access its PointEffector2D component and modify it to our liking
    48.             CelestialBodyPointEffector2D = SunObject.GetComponent<PointEffector2D>();
    49.             CelestialBodyPointEffector2D.forceMagnitude = -(SunMass);
    50.  
    51.             //----------------------------------------------------------
    52.             //---------------------------------------------------------
    53.             //---------------------------------------------------------
    54.             // Randomly generate the planets that go around the suns
    55.             //---------------------------------------------------------
    56.             //---------------------------------------------------------
    57.             //---------------------------------------------------------
    58.             int PlanetCount = SunCount;
    59.          
    60.             while (PlanetCount > 0)
    61.             {
    62.                 // Randomly generate masses between 1,000 and 5,000
    63.                 float PlanetMass= Mathf.Floor(Random.Range(1000f, 5000f));
    64.  
    65.                 // Add them to the celestialMasses List
    66.                 celestialMasses.Add(PlanetMass);
    67.  
    68.                 // Add them to the Full Mass counter
    69.                 FullMass = FullMass + PlanetMass;
    70.  
    71.                 // Create a new Vector3 that will place the object with Random X and Y coordinates
    72.                 Vector3 PlanetPosition = SunPosition + (SunObject.transform.right * 2);
    73.                 //transform.position = PlanetPosition;
    74.              
    75.                 //Vector3 pos = new Vector3(Mathf.Floor(Random.Range(-1000, 1000)), Mathf.Floor(Random.Range(-1000, 1000)), 0);
    76.  
    77.                 // Create a new gameobject that will instantiate the celestialbodyprefab at the above vector3 position
    78.                 GameObject PlanetObject = Instantiate(CelestialBodiesPrefab, PlanetPosition, Quaternion.identity) as GameObject;
    79.  
    80.  
    81.                 PlanetCount = PlanetCount - 1;
    82.                 Debug.Log(PlanetPosition);
    83.                 Debug.Log(SunPosition);
    84.             }
    85.          
    86.          
    87.             // Consider the Sun and its surrounding planets and moons finished and move on to the next
    88.             SunCount = SunCount - 1;
    89.         }
    90.  
    91.         //----------------------------------------------------------
    92.         //----------------------------------------------------------
    93.         //----------------------------------------------------------
    94.  
    95.  
    96.         // Update the UI on click.
    97.         textArea0.text = "Whole Mass: " + FullMass.ToString("n0");
    98.         textArea1.text = "SuperMassive Mass: " + SuperMassiveMass.ToString("n0");
    99.  
    100.     }
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Honestly that looks fine to me.

    Perhaps try using the Instantiate(CelestialBodiesPrefab) with the single parameter, which returns a GameObject. Then explicitly set the position afterwards with PlanetPosition.transform.position. See if that gives you a different result.

    Other than that, I would continue to debug, checking if the new PlanetObject.transform.position matches PlanetPosition after instantiation. Verify all the inputs and outputs. Is it possibly a physics interaction that takes place when these objects initialize? Perhaps their Rigidbodies should start asleep, and then you can wake them after they're all created? Seems unlikely but it's worth a try just to see if you can make the output change.

    You could also put a script on the CelestialBodiesPrefab to Debug.Log the position on Awake, Start, and first Update to see if the values are changing during initialization.
     
    PeaceLaced likes this.
  5. PeaceLaced

    PeaceLaced

    Joined:
    Jun 2, 2015
    Posts:
    53
    Jeff, thank you for your attention. The issue was definitely a physics update happening so quickly I could not see it. I had not yet implemented the planet mass so it was using the prefab default at 0.0001.

    +1 to prefab default awareness.
     
    LiterallyJeff likes this.