How to instantiate a game object and/or a prefab

I have a game object (an asteroid) and a corresponding prefab that moves horizontally. I want the asteroid (the gme object or the prefab) to appear randomly in the scene and move like any other asteroid. I have no idea about how to achieve this behavior in my game. I will very much apprectate your feedback. I am using C#.

I’m semi guessing you’re making a 2D game? Here’s a quick script to spawn an asteroid randomly in 2D.

using UnityEngine;
using System.Collections;

public class AsteroidSpawn : MonoBehaviour {
	
	public GameObject asteroidPrefab;

	void Start()
	{
        Vector3 randomSpawn = new Vector3( Random.Range( 0.0f, 10.0f ), Random.Range( 0.0f, 10.0f ), 0 );
		GameObject.Instantiate( asteroidPrefab, randomSpawn, Quaternion.identity );
	}
}

EDIT ** Let me explain a bit better, you seem a little new. Attach this script to a game object in your scene (let’s call it Asteroid Manager) and in the Inspector drag your Prefab asteroid (not the game object in the scene!) to the public variable asteroidPrefab. The script also assumes you only want the asteroid to spawn randomly on the x/y plane between 0-10 units on both the x and y axis.

Yes, this is for a 2D approach. Also, you are right, I am a novice about game development and, of course, about Unity3D.
I write to tell you that I appreciate you response very much, it was of great help. I was able to understand and deduct some other issues from this answer.

What I have noticed is that I need to include (drag and drop) one game object (asteroid) in the scene in order to be able to instanciate it. If I remove it then no random asteroids appear. I suppose there should be an approach for not having to do it this way, but I have not found it yet.

Thanks in advance for your comments about this issue,

Sorry for the delayed response. But it sounds like you aren’t using a “Prefab”, but just a single instance of a game object. A Prefab acts like a prefabricated object, or a blue print for creating objects.

In your Project view, right click and create a Prefab. Then, drag your asteroid game object from your scene onto the Prefab, this will make the asteroid a prefabricated object and save it in your project. Last, drag the instance of that Prefab object (from the Project panel) onto the script’s exposed public variable, asteroidPrefab. That should do the trick. You can then remove the asteroid game object from your scene and the “GameObject.Instantiate” static call will use the asteroid’s Prefab to create the game object.

Hope I caught you in time before you wrote too much more code!