Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

how can i store the position of my instantiated Objects

Discussion in 'Scripting' started by pachermann, Jul 8, 2017.

  1. pachermann


    Dec 18, 2013
    Hi everyone,

    i have a kinda basic question, i know i can't acces function inside my class.
    But there must be way to store the positions of my Instanciated Object.
    Its a basic question, i'am still beginner and can't figure how.

    The first funciton instanciates a object on game start, a street patch.
    Code (csharp):
    2. public class AssetResourceLoader : MonoBehaviour {
    4. public void LoadDefaultPatch() {
    6.       GameObject instanceDefaultPolyAsset = Instantiate(Resources.Load("PolyAssets/street_patch_default",
    7.       typeof(GameObject))) as GameObject;
    9.       // make it a silbling of the AssetResourceLoader GO
    10.       instanceDefaultPolyAsset.transform.SetParent(this.transform);
    11.       Debug.Log("default asset has been loaded");
    13.       Debug.Log(instanceDefaultPolyAsset.transform.position);
    14.     }
    I get te positon when i read instanceDefaultPolyAsset.transform.position.

    Another Function loadStreetPatch() does load new patches, but they need to know where the default patch is.
    Code (csharp):
    2. further down the -> public class AssetResourceLoader : MonoBehaviour {
    4. // this function must place the newly instantiated Object
    5. public void loadStreetPatch() {
    7.         // creates an instance of the loaded obj on the scene
    8.         GameObject instance = Instantiate(Resources.Load("PolyAssets/street_patch_10m_straight",
    9.         typeof(GameObject))) as GameObject;
    11.         Debug.Log("new asset has been loaded");
    13.         //The created instance needs to be silbling of the AssetResourceLoader Go in hierarchy
    14.         instance.transform.SetParent(this.transform);
    15.         instance.transform.position = new Vector3(10f, 0, 0); <--- here i want to set position
    17.         if ( instance == null )
    18.         {
    19.             Debug.LogError( string.Format( "Couldn't find GameObject street_patch_10m_straight" ) );
    20.             return;
    21.         }
    23.         Debug.Log(instance.transform.position);
    24.     }

    How can i send the position to my next function in order to place the new patches correctly?

    Last edited: Jul 8, 2017
  2. DroidifyDevs


    Jun 24, 2015
    First, please use code tags so your code is readable!

    I think what you're asking is how to access functions and objects from another script. It's pretty simple!

    For example, this is the top of one of my scripts:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class HealthInfo : MonoBehaviour {
    6.     public CharachterAnimator AnimationScript;
    I'm going to use this as an example.

    As you see, I'm accessing a CharachterAnimator class. In order to run the function, I need 1 more step. I need to reference it. There's (at least) 2 ways of doing this:
    1: Drag and drop the object with the other script (CharachterAnimator) onto the slot in the HealthInfo inspector. You'll end up with this:
    Note that "FriendSoldier1" is the object that has the CharachterAnimator script on it.

    2: You can use GetComponent in your original script (HealthInfo in my case). For example:
    Code (CSharp):
    1. void Start()
    2. {
    3.     AnimationScript = GameObject.Find("FriendSoldier1").GetComponent<CharachterAnimator>();
    4. }
    This is sometimes handy, but you should use it only when you can't drag from the Inspector (for example, you need to access a component from a spawned object while the game is running).

    Once you have it referenced, you simply call a public function!
    Code (CSharp):
    1. AnimationScript.GetAnimationType();
    If you need to set an object, you can do something like this:
    Code (CSharp):
    1. AnimationScript.OtherObject = transform;
    Note that you can access from other scripts only public objects and functions. Otherwise you'll get an "Unable to access due to protection level" error.

    That's it! :D

    P.S. I had to use my CharachterAnimator and HealthInfo example as you didn't include your class names.
    Last edited: Jul 8, 2017
    pachermann likes this.
  3. pachermann


    Dec 18, 2013
    Hi DroidifyDevs,

    thanks for your explanation, i used now the code brackets and i tried to explain it better what i wanna achieve.
    I'am loading the objects with these functions inside one class (same).
    Still i cant figure how to store the position of my loaded patch to tell the next patch where it needs to be positioned.

    maybe you can give me tip how to do this, since they are generated on runtime i think i can't use the public variable and drag the patchtes there.


  4. DroidifyDevs


    Jun 24, 2015
    Oh, they're both in the same class. That makes it easier, and also makes my previous answer useless lol

    I see this line:
    Code (CSharp):
    1.  instance.transform.position = new Vector3(10f, 0, 0); <--- here i want to set position
    What do you want the position to be? I understand instead of new Vector3 you want it to be something from the previous code clip, but I'm not sure what.

    EDIT: I'm guessing you want instance.transform.position to equal instanceDefaultPolyAsset.transform.position.
    In that case, you can do this:
    Code (CSharp):
    1. public Vector3 DefaultPolyAssetPosition;
    Put that at the top of your script before any functions, then in the first code clip:
    Code (CSharp):
    1. public void LoadDefaultPatch() {
    2.      GameObject instanceDefaultPolyAsset = Instantiate(Resources.Load("PolyAssets/street_patch_default", typeof(GameObject))) as GameObject;
    3.      DefaultPolyAssetPosition = instanceDefaultPolyAsset.transform.position; //save position for later use
    5.      // make it a silbling of the AssetResourceLoader GO
    6.      instanceDefaultPolyAsset.transform.SetParent(this.transform);
    7.      Debug.Log("default asset has been loaded");
    8.      Debug.Log(instanceDefaultPolyAsset.transform.position);
    9.     }
    Then in the 2nd code clip:
    Code (CSharp):
    1. instance.transform.position = DefaultPolyAssetPosition;
    That way you save the DefaultPolyAssetPosition every time you spawn the instanceDefaultPolyAsset, and it's available to use in other functions.
    Last edited: Jul 9, 2017
    pachermann likes this.
  5. pachermann


    Dec 18, 2013
    Hey DroidifyDevs,

    very cool this works, now it does exactly what i wanted, its saves the position inside my vector3.
    Next i go on and try to make a iteration with every new patch instanciated , to count up the vector3 axis.X.

    thank you DroidifyDevs!
    best, pascal

    DroidifyDevs likes this.