Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Simple script to set and change the skybox, fog and so on in Unity

Posted on April 19, 2025April 19, 2025 by Habibie

In this article, we will discuss how to create a script in Unity that automatically applies several important settings when the game starts. This script will enable fog, change the skybox material, destroy objects with a specific tag, and spawn a prefab at a specified location. All of these tasks will be executed when the scene starts (in Unity’s Start() method).

Here are the steps and detailed explanations of the script you can use:

1. Enabling Fog and Adjusting Its Color

Fog is a visual effect used to add atmosphere to a scene. By using fog, we can create a sense of mist or weather effects in the environment. In Unity, we can enable and adjust fog using RenderSettings.fog. Here is the basic setup for fog:

RenderSettings.fog = true; // Enable fog
RenderSettings.fogColor = fogColor; // Set fog color
RenderSettings.fogDensity = fogDensity; // Set fog density

In the script above, we enable fog and adjust its color and density. The fog color can be customized using the fogColor variable, and the fog density can be modified using fogDensity.

2. Changing the Skybox

The skybox is the background that is used to give the appearance of a vast sky or surrounding environment in the scene. Unity provides several default skybox materials, but you can also replace it with your own custom skybox material.

To change the skybox, we use the RenderSettings.skybox property and assign it the desired material. Here’s how we can do this:

if (skyboxMaterial != null)
{
RenderSettings.skybox = skyboxMaterial; // Change the skybox with the selected material
}

In this script, we check if the skybox material has been assigned by the user. If it has, the scene’s skybox will be replaced with the selected material.

3. Destroying Objects with a Specific Tag

In game development, it’s often necessary to remove certain objects from the scene, for example, objects with a specific tag. We can do this using GameObject.FindGameObjectsWithTag() to find all objects with a given tag and then destroy them using Destroy().

GameObject[] objectsWithTagA = GameObject.FindGameObjectsWithTag("A");
foreach (GameObject obj in objectsWithTagA)
{
Destroy(obj); // Destroy objects with the "A" tag
}

This script searches for all objects with the tag “A” in the scene and destroys them one by one.

4. Spawning Prefabs

After setting up the environment, the next step is to add objects to the scene, such as spawning a prefab. A prefab is a pre-configured object that can be instantiated (added) to the scene at runtime.

To instantiate a prefab, we can use the Instantiate() method:

if (prefabToSpawn != null)
{
Instantiate(prefabToSpawn, Vector3.zero, Quaternion.identity); // Spawn prefab at position (0,0,0)
}

Here, we check if the prefab has been assigned by the user. If so, the prefab will be instantiated at position (0, 0, 0) with the default rotation (Quaternion.identity).

Full Script

Here is the complete script that combines all the features mentioned above:

using UnityEngine;

public class GameSettings : MonoBehaviour
{
// Material for Skybox and Prefab that can be assigned by the user
public Material skyboxMaterial;
public GameObject prefabToSpawn;

// Fog color and density settings that can be adjusted
public Color fogColor = Color.gray;
public float fogDensity = 0.1f;

void Start()
{
// Enable fog and set its color and density
RenderSettings.fog = true;
RenderSettings.fogColor = fogColor;
RenderSettings.fogDensity = fogDensity;

// Change the skybox to the material assigned by the user
if (skyboxMaterial != null)
{
RenderSettings.skybox = skyboxMaterial;
}

// Destroy all objects with the "A" tag
GameObject[] objectsWithTagA = GameObject.FindGameObjectsWithTag("A");
foreach (GameObject obj in objectsWithTagA)
{
Destroy(obj);
}

// Spawn the prefab if it is assigned by the user
if (prefabToSpawn != null)
{
Instantiate(prefabToSpawn, Vector3.zero, Quaternion.identity);
}
}
}

How to Use This Script

  1. Add the Script to an Object in the Scene
    Drag and drop this script onto any object in the Unity scene (for example, a GameManager object).
  2. Set the Required Values
    In the Unity Inspector, you can assign the skyboxMaterial with the desired skybox material and prefabToSpawn with the prefab you want to spawn.
  3. Adjust Fog Settings
    You can change the fog color (fogColor) and fog density (fogDensity) according to the desired look of your scene.

Conclusion

This script provides an easy way to configure various elements in your game, such as fog, skybox, object destruction, and prefab spawning, all with a simple script in Unity. This way, you can streamline the setup process and create a more dynamic and immersive game environment!

Post Views: 411
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme