How to make variables local to one instance of a prefab?

My question is pretty straightforward, I am trying to find a way to create local variables that only apply to the instance of the prefab they are attached to, yet I cannot wrap my head around on how to fix it.

All variables only “apply” to a specific instance of a prefab unless they’re static. Are you making public variables and then altering them in the inspector? If so, the values in the inspector will be used regardless of any default values assigned to them in the script. However, they will still be able to be altered independently when the prefab is instantiated.

Also, make sure you are altering the the instantiated objects in the hierarchy (and not the prefab asset itself in the project tab) if you are trying to adjust the values independently during runtime. Instantiated prefabs will be marked by (Clone) after their name in the hierarchy.

If you instantiated a prefab by dragging it into the scene in edit mode, it won’t have (Clone) in its name, but it will have a different icon with blue text. Just like with the clones, you can adjust their public variables in the inspector independently of the parent prefab asset. These will also have an “overrides” option at the top of the inspector which will allow you to revert back to the original prefab, or apply the changes of the instance back to the original prefab.

would help seeing some of the code you are using to bring the prefab into the scene.

Best practice

using UnityEngine.UI;
public class MyPrefabClass : MonoBehaviour
{
    public Text textObj = null;
    [System.NonSerialized] int tileLevel = 0;
    public int GetTileLevel { get { return tileLevel; } }
    public int SetTileLevel { set { tileLevel = value; } }
}
 
using System.Collections.Generic;
public class MyControlClass : MonoBehaviour
{
    public int prefabPadding = 25;//Set to ur prefabs height
    public string prefabName = "Prefab";
    public string resources = "Prefabs/";
    public string resourcesActualLocation = "Assets/Resources/Prefabs/";
    public GameObject scrollViewContent;//Probably a vertical scroll view set up on this GameObject to create a list.
    static GameObject prefab = null;
    [SerializeField] List<GameObject> prefabsList = new List<GameObject>();

    public int GetTileLevel(int index)
    {
        //Set a default to return.
        int tileLevel = 0;
        //Make sure list isn't null or has a count
        if (prefabsList != null && prefabsList.Count >= 1)
            //Loop though whole List
            for (int i = 0; i < prefabsList.Count; i++)
                //Find Spacific Index
                if (index == i)
                {
                    tileLevel = prefabsList*.GetComponent<MyPrefabClass>().GetTileLevel;*

prefabsList*.GetComponent().textObj.text = tileLevel.ToString();*
}
return tileLevel;
}
public void SetTileLevel(int index, int newTileLevelValue)
{
//Make sure list isn’t null or has a count
if (prefabsList != null && prefabsList.Count >= 1)
//Loop though whole List
for (int i = 0; i < prefabsList.Count; i++)
//Find Spacific Index
if (index == i)
{
prefabsList*.GetComponent().SetTileLevel = newTileLevelValue;*
prefabsList*.GetComponent().textObj.text = GetTileLevel(i).ToString();*
}
}

///


/// Makes a new Game Object and instantiates it.
///

public void CreateNewPrefabScenePrefabOnList(string objectName)
{
if (prefab == null)
prefab = GetPrefabPanel;

//Create a new prefab in scene on the list object
GameObject obj = Instantiate(prefab, scrollViewContent.transform);
CreateNewPrefabScenePrefabOnList(objectName, obj, prefabPadding);
}
public void AddPrefab(GameObject prefabCopy, int newTileLevelValue = 0)
{
if (prefabsList == null)
prefabsList = new List();
prefabsList.Add(prefabCopy);
SetTileLevel(prefabsList.Count, newTileLevelValue);
}
public void RemovePrefabFromList(int Index, bool destroy = false)
{
//If list doesn’t exists stop
if (prefabsList == null)
return;

//Make empty GO in case we Find the object we can cache it.
GameObject temp = null;

//Loop list to find object and set it
for (int i = 0; i < prefabsList.Count; i++)
if (i == Index)
temp = prefabsList*;*

//Remove from list
prefabsList.RemoveAt(Index);

//And lastly destroy if destroy = true.
if (destroy)
Destroy(temp);
}
public void RemoveAllObjectFromSceneByTag(string tag)
{
//Find all prefabs in scene and collect them in a list.
// then Destroy each object in the list.
GameObject[] prefabs = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject go in prefabs)
Destroy(go);
}
public void RemoveAllPrefabsFromSceneFromSavedList()
{
foreach (GameObject go in prefabsList)
Destroy(go);
}

///


/// Loads a prefab from resources.
///

GameObject GetPrefabPanel
{
get
{
if (prefab == null)
{
prefab = Resources.Load(resources + prefabName);
#if UNITY_EDITOR
if (prefab != null)
Debug.Log(string.Format(“{2} found in {1}. [{0}]”, prefab, resourcesActualLocation, “Panel Prefab”));
else
Debug.LogError(string.Format(“{2} was not found in {1}.”, prefab, resourcesActualLocation));
#endif
}
return prefab;
}
}
void CreateNewPrefabScenePrefabOnList(string objectName, GameObject panel, int contentPadding, int tileLevelValue = 0)
{
//Increase the size of the scroll view area by the hight of the prefab [Set in inspector]
IncreaseScrollViewWindowSize(panel, contentPadding);

//Rename this object for easy finding later.
panel.name = string.Format(“{0}{1}”, objectName, prefabsList.Count + 1);

//Add and set value
AddPrefab(panel, tileLevelValue);
}
void IncreaseScrollViewWindowSize(GameObject scrollViewContent, float prefabHieght)
{
scrollViewContent.GetComponent().sizeDelta = new Vector2
(
scrollViewContent.GetComponent().sizeDelta.x,
scrollViewContent.GetComponent().sizeDelta.y + prefabHieght
);
}
void DecreaseScrollViewWindowSize(GameObject scrollViewContent, float prefabHieght)
{
scrollViewContent.GetComponent().sizeDelta = new Vector2
(
scrollViewContent.GetComponent().sizeDelta.x,
scrollViewContent.GetComponent().sizeDelta.y - prefabHieght
);
}
void ResetScrollViewWindowSize(GameObject scrollViewContent, float basePadding = 5)
{
scrollViewContent.GetComponent().sizeDelta = new Vector2
(
scrollViewContent.GetComponent().sizeDelta.x,
basePadding
);
}
}
Just a little bit of code i wrote for you for you to scan over. not sure if this is what you were looking to do or not but its a nice list generator.