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) >> Below shows the PlanetPosition as Vector3 (205.0 , 286.0 , 0.0) >> And both of them ready to print to Debug.Log >> At the bottom you can see the Debug.Log with both the Vector3 >> First clone selected has Transform that is correct. >> Second Clone shows Transform Position that is incorrect. Any help would be greatly appreciated. ~ PeaceLaced
Are both planets being spawned with that snippet in the screenshot? Is that within a loop or something?
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): public void SpwanTheSystem() { //--------------------------------------------------------------- //--------------------------------------------------------------- //--------------------------------------------------------------- // Randomly generate the sun masses and Randomly disperse them on the play field //-------------------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- // Add SuperMassiveMass to the full mass. FullMass = FullMass + SuperMassiveMass; // Create the list object that holds float numbers List<float> celestialMasses = new List<float>(); int SunCount = CelestialBodySuns; // While we still have suns to generate mass for while (SunCount > 0) { // Randomly generate masses between 100,000 and 500,000 float SunMass = Mathf.Floor(Random.Range(100000f, 500000f)); // Add them to the celestialMasses List celestialMasses.Add(SunMass); // Add them to the Full Mass counter FullMass = FullMass + SunMass; // Create a new Vector3 that will place the object with Random X and Y coordinates //Vector3 SunPosition = new Vector3(SunCount * 2, SunCount * 2, 0); Vector3 SunPosition = new Vector3(Mathf.Floor(Random.Range(-100 , 100)), Mathf.Floor(Random.Range(-100 , 100)),0); // Create a new gameobject that will instantiate the celestialbodyprefab at the above vector3 position GameObject SunObject = Instantiate(CelestialBodiesPrefab, SunPosition, Quaternion.identity) as GameObject; // Access its RigidBody2D compoenent and modify it to our liking CelestialBodyRigidBody2D = SunObject.GetComponent<Rigidbody2D>(); CelestialBodyRigidBody2D.mass = SunMass; CelestialBodyRigidBody2D.drag = 10; //ToDO: possibly modify this based on Mass??? // Access its CircleCollider2D component and modify it to our liking CelestialBodyCircleCollider2D = SunObject.GetComponent<CircleCollider2D>(); CelestialBodyCircleCollider2D.radius = SunMass/40000; // Access its PointEffector2D component and modify it to our liking CelestialBodyPointEffector2D = SunObject.GetComponent<PointEffector2D>(); CelestialBodyPointEffector2D.forceMagnitude = -(SunMass); //---------------------------------------------------------- //--------------------------------------------------------- //--------------------------------------------------------- // Randomly generate the planets that go around the suns //--------------------------------------------------------- //--------------------------------------------------------- //--------------------------------------------------------- int PlanetCount = SunCount; while (PlanetCount > 0) { // Randomly generate masses between 1,000 and 5,000 float PlanetMass= Mathf.Floor(Random.Range(1000f, 5000f)); // Add them to the celestialMasses List celestialMasses.Add(PlanetMass); // Add them to the Full Mass counter FullMass = FullMass + PlanetMass; // Create a new Vector3 that will place the object with Random X and Y coordinates Vector3 PlanetPosition = SunPosition + (SunObject.transform.right * 2); //transform.position = PlanetPosition; //Vector3 pos = new Vector3(Mathf.Floor(Random.Range(-1000, 1000)), Mathf.Floor(Random.Range(-1000, 1000)), 0); // Create a new gameobject that will instantiate the celestialbodyprefab at the above vector3 position GameObject PlanetObject = Instantiate(CelestialBodiesPrefab, PlanetPosition, Quaternion.identity) as GameObject; PlanetCount = PlanetCount - 1; Debug.Log(PlanetPosition); Debug.Log(SunPosition); } // Consider the Sun and its surrounding planets and moons finished and move on to the next SunCount = SunCount - 1; } //---------------------------------------------------------- //---------------------------------------------------------- //---------------------------------------------------------- // Update the UI on click. textArea0.text = "Whole Mass: " + FullMass.ToString("n0"); textArea1.text = "SuperMassive Mass: " + SuperMassiveMass.ToString("n0"); }
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.
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.