object respawn script

public class spawnItems : MonoBehaviour { public Transform[] SpawnPoints; public float SpawnTime=4.0f; public GameObject StealthMush; // Use this for initialization void Start () { InvokeRepeating ("SpawnMush",SpawnTime,SpawnTime); } // Update is called once per frame void Update () { } public void Stop() { CancelInvoke("SpawnMush"); } public void SpawnMush() { int spawnIndex = Random.Range (0,SpawnPoints.Length); Instantiate (StealthMush,SpawnPoints[spawnIndex].position, SpawnPoints[spawnIndex].rotation); } } 

interaction script with some items

 public class hand : MonoBehaviour { public Transform player; public Transform mushrums; public Transform mushrumsInv; public Transform fastRun; public Transform beastFear; private Animator anim; GameObject closest; public GameObject invisibility_btn; public GameObject fastRun_btn; public GameObject fear_btn; public spawnItems stoprepeat; void Start () { anim = GetComponent<Animator> (); } GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("mushrums"); //GameObject closest; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } public void HandUp () { float dist = Vector2.Distance(player.position, mushrums.position); float distInv = Vector2.Distance(player.position, mushrumsInv.position); float distRun = Vector2.Distance(player.position, fastRun.position); float distFear = Vector2.Distance(player.position, beastFear.position); Debug.Log(mushrums.position); Debug.Log(mushrumsInv.position); Debug.Log(distRun); Debug.Log(distFear); if (dist <= 59.0f) { anim.Play("PickUp"); Destroy(FindClosestEnemy()); }; if (distInv <= 21.0f) { anim.Play("PickUp"); stoprepeat.Stop(); Destroy(FindClosestEnemy()); invisibility_btn.SetActive(true); }; if (distRun <= 5.0f) { anim.Play("PickUp"); //Destroy(FindClosestEnemy()); fastRun_btn.SetActive(true); }; if (distFear <= 21.0f) { anim.Play("PickUp"); fear_btn.SetActive(true); }; } } 

How to correctly transfer the coordinates of an object from a respawn instance on a mushrumsInv object to an interaction script?

    1 answer 1

    Several options. It all depends on where else these coordinates may be needed and how unique they are. You can make a static variable and pass through it. You can use the messaging system. You can search for a GameObject on the stage, and in it GetComponent <script_name> () .name_variable.

    "Competently" is a relative thing. It is better to build on the architecture that you are laying and which design patterns you are already using.

    I, I confess, did not fully understand the logic in your code, it would be good if you isolated the very essence of the question from it, but you can try one of the options I offer.