How to teleport the player to the location of an instantiated projectile?

I want the player to be able to fire three slow moving projectiles with the right mouse button and then teleport to them, one after the other (as long as they still exist) using the left mouse button. This is presenting two specific challenges for me. First, I don’t know how to access/store the position of the instantiated projectiles, so that my player can teleport to them.

You’ll see below that I tried using a tag to find the object but I’m very new to Unity and C# and I am having difficulty.

Secondly, I want to have it so that there can be a maximum of three projectiles at any point. Creating a new projectile once there are already three should destroy the oldest and replace it with the new one. The tricky part for me (even the logic) is that if, say the second projectile is prematurely destroyed (by say colliding with an object), how do I maintain the proper order when adding a new one. In other words, I always want to teleport to the oldest projectile.

Eg. I fire all three projectiles. Projectile1 and Projectile3 both still exist but Projectile two was triggered when it collided with an object and destroyed itself. Now I fire another projectile. I need it to be come after Projectile1 and Projectile3 so that they’ll be used first.

After doing some research I’m thinking an array may be the answer but again, I’m just too new to this to know quite how to approach the problem.

using UnityEngine;
using System.Collections;
 
public class FireSynapse : MonoBehaviour {
 
    public float cooldown = 1f;
 
    float cooldownRemaining = 0;
 
    public GameObject SynapsePrefab;
 
    //used for initialization
 
    void Start () {
 
    }
 
    //update called once per frame
    void Update () {
       cooldownRemaining -= Time.deltaTime;
 
       if (Input.GetMouseButtonDown(1)  cooldownRemaining <= 0) {
         cooldownRemaining = cooldown;
 
         Instantiate(SynapsePrefab, Camera.main.transform.position, Camera.main.transform.rotation);
       }
 
       if (Input.GetMouseButtonDown (0)) {
         GameObject Portal = GameObject.FindWithTag("Portal");
         Debug.Log (transform.position = new Vector3(SynapsePrefab.transform.position.x, SynapsePrefab.transform.position.y, SynapsePrefab.transform.position.z));
       }
    }
}

Thank you in advance for your help.

I would make sure your player knows the projectiles, it has the transforms of each stored in memory. Then, on mouse click, get position of the appropriate one and set position to it. If transform position null, check a different one.

First, thank you so much for the fast reply.

Could you elaborate on that sentence a bit for me? How would I make sure the player knows the projectiles? Are you saying the transforms are automatically stored somehow?

Thanks again.

Ok,So you are instantiating the projectiles. So, I would create a List.

using Systems.Collections.Generic;
public List<Transfrom> projectilesList;


void whenUCreateRockets(){
rocket = Instantiate();
projectilesList.Add(rocket);
}

The above code shows how you can add an object to a list. Now, you have stored values of a transform class which you can access any member of.

You can also remove these items from the list, so the list is always an accurate portrayal of the rockets that exist in scene.

This is how you track them.

Keep in mind you should remove them from the list when they die.
As such each rocket will need to acces this mananger.

void killRocket(){
AccessorToScriptThatCreatesRockers.projectilesList.Remove(this);
///now you are free to destroy this object.
Destroy(gameObject);

Replace ‘this’ with gameObject and it might be fine, since ‘this’ represents the script and not the gameObject.

@ Stratlin

You said you want to teleport to them one after the other, and later you said, you always want to port to the oldest.
If you want to stick to the first version have a look at this situation:

You teleported to the oldest.
Then you teleported to the second oldest.

The next one would be the 3rd one, but if second one has been destroyed meanwhile, would you want to go back to the oldest or still stick to the order and go to the one which was 3rd and is now 2nd (since the old 2nd has been destroyed)?

Well i just noticed there are even more cases where i don’t know which item you’d want to go next. xD

It’s a nice little challenge though, but not hard if you know all the conditions.

Thanks guys, I’ll give this a try.

You’re hitting on what’s confusing me logically, Suddoha. If I fire portalA, then PortalB, then PortalC… and portalB is the first to be destroyed, would portalC become portalB and my next portal be a new portalC? Otherwise, if I just fill the now vacant portalB, the chronological order won’t be right.

What I want is that I always use the oldest portal still remaining.

edit (forgot about the below quote):

I’m not sure I understand what “this” you are referring to. Sorry.

Thanks again.

Thank you both for your help, I really appreciate it. This was my first day using the Unity community for help so when I tried the Questions instead of the forums my post was held up for a bit so a moderator could make sure it was legit.

I did get a solution that solves all of the issues (or seems to). It used a list as you suggested and can be found here:
http://answers.unity3d.com/questions/700576/how-to-teleport-the-player-to-the-location-of-an-i.html

Thanks again everyone. It’s been a rough day of programming for me but a great introduction to the Unity Community.