- Home /
Clone wants a Parent (c#)?
How would you go about moving a clone to the main camera parent. Or should I say how would you turn the new clone into a child of the new Parent (Main Camera). It's a c# script.
// Start Cloning Planets to create the "visit Planet Buttons" for the Main Camera.
if (GameObject.Find(SystemSizeSetup[i].name + "Sphere")){
var clone = Instantiate(GameObject.Find(SystemSizeSetup[i].name + "Sphere")); // Clone Planets
clone.name = "c" + SystemSizeSetup[i].name; // Rename Clones
} // End Cloning
The clones will line up from top to bottom, then you will be able to choose which planet to visit next. This way no matter where you go with the camera, the planets will always be lined up with the camera.
Thx, David.
P.S. is "var" not a java command, why is it working in c#? What would be the c# version?
var is a C# keyword as well. It denotes any type, but variable declared using var must be initialized immediately and is strongly typed. For example, you can't do:
var number;
number = 1;
but ins$$anonymous$$d you have to use:
var number = 1;
This declares number variable which is of int type. But if you do:
var number = "123";
then number will be string.
Neat, this will help a lot, I usually ignored any var data which I though was Java, since I was concentrating on learning c# only and which is faster then Java. So many languages out there, first I learned Basic, then Lingo, now c# and english :-)
Answer by MissingSemicolon · Sep 03, 2013 at 03:34 PM
Something along the lines of:
clone.transform.parent = Camera.main.transform;
That should parent your new clone to the main camera in the scene (whichever camera has been tagged "MainCamera")
Thanks a lot, That looks easy enough, but I get an error message saying that is does not contain a definition for transform. I copied and pasted into the script after clone.name. I also tried "clone.transform.parent = myCamera.transform" since I have the transform defined for myCamera, it didn't work either. I the script I have "public Transform myCamera;" in which my $$anonymous$$ain Camera is connected. What am I missing?
Replace the var with GameObject, so it will look something like this:
GameObject clone = Instantiate(GameObject.Find(SystemSizeSetup[i].name + "Sphere")) as GameObject; // Clone Planets
I haven't tested this but it should be along the right lines.
myCamera is already a transform so :
clone.transform.parent = myCamera;
should do
clone.transform.parent = myCamera; // This didn't work, but still like to know why it didn't. $$anonymous$$eeps saying there is no transform set.
But this line solved it all GameObject clone = Instantiate(GameObject.Find(SystemSizeSetup[i].name + "Sphere")) as GameObject; // Clone Planets Thanks a lot How does the moving part work anyways, does it first create a clone, then renames it, and then move the renamed clone to the camera, or does it get done all at once till the first loop is complete, then returns to the beginning of the loop until its done with its routine.
The reason is probably that you were created an object of type Object which is what Instantiate returns. Then Object does not have a Transform. Doing the as GameObject cast the object into GameObject which now has a Transform. That is what I think.
This is the danger of using UnityScript vs C#. C# would not have allowed you to do that because the reference would have been a GameObject.