Hello,
I’m looking for some help because I can’t find out how I can get it done.
I want to make collectables in my game. I made different spawning points and I have made one collectable.
After 1 collectable has been picked up I want it to respawn on one of the other spawnpoint (in random) and so on. Can someone help me out with the code?
Show us what you got, show us where in what you got your having a hard time.
Wow thats a fast reply 
I’ve made a game object (Itemspawn) with different locations into it. And added those locations to the gameobject.
public class PlayerPickup : MonoBehaviour
{
public GameObject[] Spawnlocations;
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
***instead of use setActive(false) I want it to move to one of the GameObject SpawnLocations***
}
}
Im not that good yet in C# but I thought this might work. I’ve got this code from the tutorial from Unity. I tried a lot of solutions from the internet but none of them work. Also tried other solutions I’ve found but without any result.
So from the looks of this code it appears you have it attached to the player… I say this because you check if “other” has a “Pick Up” tag. Also because you have that FixedUpdate.
It might be better to have it attached to the pickup itself. This way you can have multiple pickups that can be moved around.
Remove that FixedUpdate, that should be in some PlayerMovement script… not the pickup script. Don’t mix behaviour into a script… a pickup script should deal with picking things up, NOT with movement of the player.
Next… you’re going to need to track at which spawn location you’re currently at so you can move to the next. Since this will now be attached to the pickup itself you can just do this on start… pick the starting spawn point and move there.
Then OnTriggerEnter you check if it was the player that entered… and if it was you iterate to the next spawn location.
Something like this:
//This needs to be attached to the pickup, not the player
public class PlayerPickup : MonoBehaviour
{
public GameObject[] Spawnlocations;
public float speed;
private Rigidbody rb;
private _currentIndex = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
this.MoveToLocation(0); //move to the first location
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player")) //I just used a generic Player tag... you can use whatever to identify the player
{
this.MoveToLocation(_currentIndex + 1);
}
}
private void MoveToLocation(int index)
{
if(index < 0 || index >= Spawnlocations.Length)
{
//uh oh, we left the range of locations... what do? delete self? signal something? Return to 0th position?
return;
}
_currentIndex = index;
this.transform.position = Spawnlocations[index].transform.position;
}
}
Something like that…
NOTE - I wrote this in the webpage here… I didn’t do spellcheck or anything. It should server more as pseudo-code to point you in the right direction rather than as an actual implementation.
1 Like
Many thanks again for the fast reply 
When I insert the code I get an error on _currentIndex (the name current index doesn’t exist in the current context)
And in the error in unity it tells me unexpected symbol and it shows me the current index in C# again
EDIT: Fixed it. Added an int before _currentIndex
Going to test it now 
DUDE your awsome!! Worked on this for so long. It works now! 
