Search Unity

Set Script Property of PreFab To GameObject Instance

Discussion in 'Scripting' started by SROSeaner, Feb 13, 2015.

  1. SROSeaner

    SROSeaner

    Joined:
    Feb 13, 2015
    Posts:
    3
    I am very new to Unity and am going through tutorials. I tried to research this problem, but not sure how to ask it because I am so new to Unity, so I am not getting the answers I require.

    Learning right now how to move game objects relative to one other.

    I have a sphere in the center, and wish to instantiate other small sphere's that use RotateAround to basically orbit center sphere.

    I created the "Small Sphere" as a prefab, and then instantiate a bunch of them on "Center Sphere's" Start method.

    I have in my editor an Empty GameObject which holds my "Center Sphere" for now

    My PreFab for the "Small Sphere" has a public GameObject property of which I would drag the "Center Sphere" into to reference it's position, but I can't drag and drop "Center Sphere" into the PreFab.

    So if I add a Rigidbody to my "Empty Game Object", gravity causes the "Center Sphere" to fall to infinity and my "Small Sphere's" keep rotating around the original point.

    I thought I could tell the "Small Sphere's" in their "Update" method to update the point they rotate around based on the transform position of the "Center Sphere". But my dilemma is I cannot figure out how to reference the position of the "Center Sphere".

    Code (CSharp):
    1. // Sample Update method from my "Small Sphere" prefab
    2. void Update()
    3.     {
    4.                 // Want to create my point based on the
    5.                 // transform position of the Centre Sphere.
    6.                 // How can I reference Center Sphere here?
    7.         Vector3 point = new Vector3 (0, 0, 0);
    8.        
    9.         transform.RotateAround(point,axis,angle);
    10.     }
     
  2. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    78
    Hey buddy, not totally sure I'm understanding what you're after but what I got from it is:

    You need to access the scripts on the instantiated spheres to provide them with the Main (Center) sphere's location, so they can rotate around it independantly?

    Assuming I'm right, you should be able to do it through script with something like this (in the place that you instantiate the smaller spheres).

    Code (CSharp):
    1. //Create a temporary gameobject to hold the instantiated items, so you can have easy temporary access to them
    2. GameObject instantiatedSphere = (GameObject)Instantiate(smallerSpherePrefab, desiredPosition, desiredRotation);
    3.  
    4. //Because you assigned it to a temporary variable when instantiating, you can access it's components - most importantly it's scripts
    5.  
    6. instantiatedSphere.GetComponent<SmallerSphereScriptGoesHere>().centerSpherePosition = transform.position;
    Is this along the lines of what you were after?
     
  3. SROSeaner

    SROSeaner

    Joined:
    Feb 13, 2015
    Posts:
    3
    Hi Velcrohead! Yes, you interpreted my jibberish correctly :)

    I tried that, but I still have the same result.

    Now, here is some more detail. So I would like to try moving the center sphere with keyboard input, and the orbiting "Small Sphere's" that are instantiated continue to orbit wherever I move the "Center Sphere". Picture it like I'm moving the Sun around in space, and all the planets orbiting it follow along holding their orbit.

    I added a Rigidbody to the "Center Sphere", and when I turn on gravity (to see what happens), the "Center Sphere" drops away and all "Small Spheres" continue to orbit around the original location even with the use of the GetComponent method.

    So it didn't change, so it seems.

    Any more ideas? Maybe my whole design up front is just wrong creating more headaches for me.
     
  4. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    78
    It's probably because the instantiated spheres aren't children of the main sphere, so they don't inherit it's transform :). I'm not quite certain how to instantiate a gameobject as a child of another, but I think to fix your problem the code would look something like this (carrying on from last time's code):

    Code (CSharp):
    1. instantiatedSphere.transform.parent = centerSphere.transform;
    Just after you've instantiated it.
     
  5. SROSeaner

    SROSeaner

    Joined:
    Feb 13, 2015
    Posts:
    3
    Hi Velcrohead! That's the trick. I tried assigning the center sphere transform to the parent property as you have showed and that solved everything.

    So simple too... I feel like such a n00b. haha

    Thanks again.
     
  6. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    78
    No problem buddy, glad to be of help!