What var would you use for "Instantiate"

In my game, im trying to make a function where a cloud comes up, but the sprite stays down to look like the sprite is falling.

public Rigidbody2D cloudRb;
oof;

void CreateCloud()
{
oof = Instantiate(cloudImage, new Vector2(Random.Range(-23.42, 12.03), -19), Quaternion.identity);
cloudRb.velocity = new Vector2(0, speed);
Invoke(“DestroyCloud”, 4);
}

void DestroyCloud()
{
    Destroy(oof);
}

my var “oof” is trying to get the Instantiate var so the destroy function can destroy the Instantiated part. Is there a easier way to do this? Or am i doing something wrong. This is only my 2nd game on unity so im very knew. Please help if you can :smiley:

If you want to reference an instantiated object, you need to do something like this: GameObject Foo = Instantiate(Object)

since you are using ‘oof’ only during the method it is created in, I would declare it inside the method.

Also, there is no need to use invoke to delay the destroy method. It will take a float as parameter which will delay the destroy by that many seconds.

public Rigidbody2D cloudRb; 

void CreateCloud() 
{ 
    GameObject oof = Instantiate(cloudImage, new Vector2(Random.Range(-23.42, 12.03), -19), Quaternion.identity); 
    cloudRb.velocity = new Vector2(0, speed); 
    Destroy(oof, 4f);  
}

If you want to know more about the destroy method, I have a full write up on my website.

@mrmatt1877 i did your code and this popped out as an error:

Assets\CloudMove.cs(43,83): error CS1503: Argument 2: cannot convert from ‘double’ to ‘float’