So i got this cool idea for a game where you shoot cubes at your opponent to knock them of the platform. I got quite a bit of issues with this script and i just can’t figure out why. I am trying to make a cube spawn infront of the player, an
d than i want a cooldown to follow.As it says, you have no variable named “player”. How do you expect it to find the player? Or put another way, how is the script supposed to know what “player” refers to? You need to declare it, probably as a class variable:
public GameObject player;
And then assign it somehow. Without doing any additional code you can drag an object into the “slot” that this line creates in Shooting’s Inspector panel.
As for spawnPos and playerRotation, you’ve declared them locally in Update(), but you’re trying to use them in Fire(). You’ll have to either add them as parameters to Fire(), like this:
StartCoroutine(Fire(spawnPos, playerRotation) );
...
IEnumerator Fire(Vector3 spawnPos, Quaternion playerRotation) {
Or make them class variables instead of local:
Vector3 spawnPos;
void Update() {
spawnPos = playerPos + playerDirection * spawnDistance;
}
(Note that the Vector3 before the spawnPos within the function has been removed - if you don’t remove that you’ll have both a class and local variable, and you’ll only be modifying the local one)
thanks bro you really know your stuff. To be honest i felt so lost, i was probebly close to going insane haha. It works fine now but the thing is my “timer” between the shots is not working at all. It basicly just spawns in so many cubes that the whole thing just turns into a rocket.