- Home /
How to manipulate a variable of a prefab script (instantiated) while the game is runnning .
This is kind of hard to explain but I hope you all can understand me... I have this project where the user (player) can manipulate some characteristics of the objects on scene, when they click on it, a UI menu is opened and I want them to have the option to change a variable of that gameobject script by interacting with the menu. And, these objects are also prefabs instantiated by the player on scene, and there will be multiple clones of the same prefab, where the player can manipulate each one separately by clicking them. The thing is, how can I get access to that variable in the prefab script in order to be changed when the object is clicked. All of this of course when the game is running. Click detection script:
public class Seleccionar : MonoBehaviour
{
public GameObject cambiarCargaUI;
public Text cargaText;
private pCargada carga;
private void Start()
{
carga = GetComponent<pCargada>();
cambiarCargaUI.gameObject.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.transform.gameObject.tag == "Particula")
{
//
//Acces to the gameObject script and manipulate a specific variable of it.
//
cambiarCargaUI.gameObject.SetActive(true);
Debug.Log("Tocaste: " + hitInfo.transform.gameObject.name);
}
else
{
cambiarCargaUI.gameObject.SetActive(false);
}
}
}
}
GameObject script to access:(Variable: carga)
public class pCargada : MonoBehaviour {
public float carga = 1f;
public Material matProton;
public Material matElectron;
public Material matNeutro;
private void Start()
{
GetComponent<Renderer>().material = matNeutro;
}
What do you recommend me to do or investigate to achieve it? I have researched a lot and I haven´t found anything alike, now I don´t even know how to search for it. If you know how to do it or do you have any advice, what should I do?
Answer by ADiSiN · Jul 14, 2019 at 12:48 AM
I will provide you simple example with only 1 variable so you can see the one of possible solutions to achieve it and implement it in your project.
So, we will have 3 scripts ( you can actually make it in 2 or 1 scripts, but it is better to keep functions separated by logic in different scripts, at least will be easier to navigate for you in future)
This is the first script. You can put it on anything you want it should be only 1 in game.
using UnityEngine;
public class ClickDetecter : MonoBehaviour
{
public GameObject UICanvas; // Canvas that pop-up when we click on object to adjust variables
public ChangeParameters changeParameters; // Script to help us transfer data and communicate with selected object
Camera c_Cam; // Camera
private void Start()
{
c_Cam = Camera.main; // Assigning camera - Optimization purpose, because it is better not to use Camera.main each update
}
void Update()
{
if (Input.GetMouseButtonDown(0)) // Check for left mouse button input
{
RaycastHit hit;
if(Physics.Raycast(c_Cam.ScreenPointToRay(Input.mousePosition), out hit))
{
if (hit.transform.CompareTag("Particula")) // Check if clicekd object has our aiming tag - Compare.tag has better perfomance than gameObject.tag comparison
{
UICanvas.SetActive(true); // Enable UI canvas to adjust variables
changeParameters.LoadNewData(hit.transform.gameObject); // Transfer hitted object to our another script
}
}
}
}
}
So, basically, we just check if we hit object with right tag and if we do then we enable UI canvas where we will adjust variables and send to our second script our hitted gameobject.
using UnityEngine;
public class ChangeParameters : MonoBehaviour
{
public float f_size;
GameObject g_SelectedObject;
InstatiatedParamateres data;
Vector3 v3_StartScale;
/* Take and assign variables */
public void LoadNewData(GameObject selectedObject)
{
data = selectedObject.GetComponent<InstatiatedParamateres>();
g_SelectedObject = selectedObject;
v3_StartScale = selectedObject.transform.localScale;
f_size = data.size;
}
/* When player hit + or - button */
public void ChangeSize(int amount)
{
f_size +=amount;
g_SelectedObject.transform.localScale = v3_StartScale * f_size;
}
/* When player hit save button - we save changed data */
public void SaveData()
{
data.size = f_size;
}
/* When player click cancel button - we reset scale and also private variables*/
public void CancelChanges()
{
g_SelectedObject.transform.localScale = v3_StartScale * data.size;
f_size = data.size;
}
}
This is where we do all changes. We assign variables and create functions to be able adjust variables via UI buttons, for example. Also we can ahve function to save data or to reset all changes. This script you can also put on whatever you would like and you need only 1 in game.
Our GameObjects will have simple script with data.
using UnityEngine;
public class InstatiatedParamateres : MonoBehaviour
{
public float size = 1f;
}
So, the logic is that we have gameobjects with data and with our click we send that data to our main script where via UI we do changes.
Feel free to ask if something unclear.