I have a game object for a spawn point for an object.
OnStart() I instantiate the object it spawns. When the player collides with the spawned object I want the spawn point to start counting and spawn another object after x amount of time.
How do I call upon the correct spawn point to start counting when the player “picks up” the spawned object?
The collison of the player and the object is used to “Pick Up” the object. I do not collide with the spawn point. Is that the only way to get the correct spawn point is by using a collider on the spawn point?
Or can reference the spawn point some way else?
Hello,
My scripting isn’t that great and I have only started coding the last few weeks, but I’ll give this a go for you. It seems you just need a function on the spawn point to instantiate a new object after waiting a certain period of time. You can access components on different gameObjects then cal functions from there. I am not sure of any of this code and I have written it all from memory so don’t take it as gold dust :-p if anyone else can correct it would help me too!
Create a new tag called “SpawnPoint”. Then tag your spawn point with that tag. On your spawn point I would attach this script:
Script name: spawnPointScript - make sure the script name is exactly this as we reference to it later on
function Start () {
// here insert your code to instantiate the object - i presume you already have this from your question
}
function spawnObject () {
yield WaitForSeconds (10); // put the amount of seconds you wish to wait before spawning another object
// here insert your code to instantiate the object
}
The object you spawn will need a collider and I believe a rigidboby attached to it. Create a new tag and name it SpawnedObject and tag the object with this. Now on your player attach the below script:
var spawnPoint : Transform;
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "SpawnedObject") {
Destroy(hit.gameObject); // this will destroy the object. if you dont want to do this then just take out this line of code
Debug.Log("You've picked up the object!"); // this is just to check to see if it's worked
// all of this code I'm not sure on but this is the part im least sure about lol
newSpawn = spawnPoint.GetComponent(spawnPointScript);
newSpawn.spawnObject (); // this triggers the function in the spawnPointScript and should instantiate a new object after the set time
}
}
Now drag and drop the spawnPoint transform onto the varible in the inspector.
I think that should work but as I said at the top, I have only coded in unity for a few weeks and never had any previous coding experience apart from teaching myself lol
Hope this helps anyways…let me know.
Cheers,
Bernie