- Home /
How can i get access to a script from other scripts on other GameObjects ?
I have a script that is kind of global should be in the idea. I'm creating some GameObjects over the Terrain.
The problem is that making GetComponent will work only if two scripts are on the same GameObject. So the script that create the GameObjects is on a GameObject but i have more scripts on other GameObjects and i wonder how can i get access to the script from other scripts ? Without using GetComponent.
This is the script that create the GameObjects:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstantiateObjects : MonoBehaviour
{
public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;
public int objectsToInstantiate;
public bool parent = true;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private List<GameObject> prefabs = new List<GameObject>();
public void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
generateObjectOnTerrain();
}
public void generateObjectOnTerrain()
{
for (int i = 0; i < objectsToInstantiate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
if (parent)
objInstance.transform.parent = this.transform;
prefabs.Add(objInstance);
}
}
public List<GameObject> PrefabsList()
{
return prefabs;
}
}
Now if i want to do on another script:
InstantiateObjects gos = GetComponent<InstantiateObjects>();
I need that both scripts to be on the same GameObject. But if i have a third script on another GameObject and i want also to get access to InstantiateObjects ?
For "global" scripts, which is not actually a thing in C#, we use what is called the Singleton pattern. Essentially it works like this.
public class $$anonymous$$yGlobalClass : $$anonymous$$onoBehaviour
{
public static $$anonymous$$yGlobalClass Instance;
public string WhateverData;
void Awake()
{
if (Instance != null)
{
if (Instance != this)
{
Destroy(Instance);
Instance = this;
}
}
else
{
Instance = this;
}
}
}
Now for a non-$$anonymous$$onoBehaviour, IE a non-Unity object class, we use singleton a bit differently.
public class $$anonymous$$yOtherGlobalClass
{
public static $$anonymous$$yOtherGlobalClass Instance
{
get
{
if (instance == null)
{
instance = new $$anonymous$$yOtherGlobalClass();
}
return instance;
}
}
private static $$anonymous$$yOtherGlobalClass instance;
private $$anonymous$$yOtherGlobalClass()
{
myGlobalInteger = 5;
}
public int myGlobalInteger;
}
then from your other classes. you can call out to this as easy as
public class myClass : $$anonymous$$onoBehaviour
{
public void SetSomething()
{
$$anonymous$$yGlobalClass.Instance.WhateverData = "whateverValue";
}
}
Answer by toddisarockstar · May 20, 2017 at 08:06 AM
InstantiateObjects otherscript;
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
otherscript=objInstance.GetComponent<InstantiateObjects>();
otherscript.somevariable= "whatever";