how can i store the position of my instantiated Objects

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.

public class AssetResourceLoader : MonoBehaviour {

public void LoadDefaultPatch() {

      GameObject instanceDefaultPolyAsset = Instantiate(Resources.Load("PolyAssets/street_patch_default",
      typeof(GameObject))) as GameObject;

      // make it a silbling of the AssetResourceLoader GO
      instanceDefaultPolyAsset.transform.SetParent(this.transform);
      Debug.Log("default asset has been loaded");

      Debug.Log(instanceDefaultPolyAsset.transform.position);
    }

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.

```
**further down the -> public class AssetResourceLoader : MonoBehaviour {

// this function must place the newly instantiated Object
public void loadStreetPatch() {

    // creates an instance of the loaded obj on the scene
    GameObject instance = Instantiate(Resources.Load("PolyAssets/street_patch_10m_straight",
    typeof(GameObject))) as GameObject;

    Debug.Log("new asset has been loaded");

    //The created instance needs to be silbling of the AssetResourceLoader Go in hierarchy
    instance.transform.SetParent(this.transform);
    instance.transform.position = new Vector3(10f, 0, 0); <--- here i want to set position

    if ( instance == null )
    {
        Debug.LogError( string.Format( "Couldn't find GameObject street_patch_10m_straight" ) );
        return;
    }

    Debug.Log(instance.transform.position);
}**

```

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

Best,
Pascal

First, please use code tags so your code is readable! https://forum.unity3d.com/threads/using-code-tags-properly.143875/

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthInfo : MonoBehaviour {
    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:
3137570--237924--upload_2017-7-8_17-23-46.png
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:

void Start()
{
    AnimationScript = GameObject.Find("FriendSoldier1").GetComponent<CharachterAnimator>();
}

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!

AnimationScript.GetAnimationType();

If you need to set an object, you can do something like this:

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! :smile:

P.S. I had to use my CharachterAnimator and HealthInfo example as you didn't include your class names.

1 Like

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.

best,
pascal


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

I see this line:

 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:

public Vector3 DefaultPolyAssetPosition;

Put that at the top of your script before any functions, then in the first code clip:

public void LoadDefaultPatch() {
     GameObject instanceDefaultPolyAsset = Instantiate(Resources.Load("PolyAssets/street_patch_default", typeof(GameObject))) as GameObject;
     DefaultPolyAssetPosition = instanceDefaultPolyAsset.transform.position; //save position for later use

     // make it a silbling of the AssetResourceLoader GO
     instanceDefaultPolyAsset.transform.SetParent(this.transform);
     Debug.Log("default asset has been loaded");
     Debug.Log(instanceDefaultPolyAsset.transform.position);
    }

Then in the 2nd code clip:

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.

1 Like

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

1 Like