Guess this question was asked several times. I‘ve also found much stuff about it - but - nothing worked so far for me.
I just want to ask this question in general:
Is there a good way to change a value of a variable in another script(B) while using a script(A). Something like the GetComponent function for getting stuff from another script JUST the other way.
To get some background:
I have a script (B) that holds a function that is activated by a button.
Overall it deactivates a „sales“ shield and builds a house by creating s new gameobject on the chosen building area.
So far everything works.
Every single building area has a script(A) attached that holds details about the building area.
F.e. owner, building type …
So after i „bought“ the spot and build a house on it, it should change the Owner value in the specific script from 0 to 1.
I guess i could do it from script(B) the next time i activate it, but id love to have the value changed within the same function where i build the house script(A).
Im not on my computer for the next few hours so - sorry i cant provide any code, hope you can help me with the problem just with this little background knowledge.
It was a bit difficult to follow what you wrote, but as you mentioned. This has been asked and answered several times. You have to get a reference to the script and if the variable is public, you could change it directly or you could have public method that handles changing it. Or even a property. It depends on your use case. Generally speaking, you’d just use GetComponent to get a reference to the script on the GameObject.
Thanks so far and sorry i thought my statement was clear (typical for me^^).
So if GetComponent also works in this way i have an error anywhere in my code. Tried it several times but cant remember the Error right bow.
But overall its good to hear again that it is this code which is used for it.
I stopped trying because i wasnt sure anymore about if i do right.
If i cant solve it today ill provide you with my code.
Hope you‘d help me if thats the case:)
You can only change public variables from other scripts. The most simple solution (and the most closely tight) is just this (if you have two scripts, ScriptA and ScriptB both inherit from MonoBehaviour):
public class ScriptB : MonoBehaviour
{
public float otherVar = 0f;
void Update()
{
Debug.Log(otherVar);
}
}
public class ScriptA : MonoBehaviour
{
[SerializeField] ScriptB otherScript;
void Start()
{
otherScript.otherVar = 3.14f;
}
}
When you attach ScriptA to a GameObject, you need to have the other GameObject with the ScriptB on it and just drag and drop the proper GameObject into the inspector field where it says you need ScriptB. You can drag and drop GOs with the proper components on them and Unity will maintain the connection with the Component.
Or you can GameObject.Find (or any other way) the proper GameObject and find the component with the GetComponent() method from code.
Mh i think i got it. I tried to GameObject.Find in my function.
As far as i know this won’t work?! Doesnt it?! If so i need to redo my whole Codes because i access my chosen GameObjects and its script by a variable that stores the GameObjects name (from my raycast script).
But as i said ill provide you with my codes.
ScriptA (where i have my function):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuyBuildFunctions : MonoBehaviour
{
public GameObject Controller;
public string PlotObjectName;
public GameObject PlotToBuild;
private Vector3 ChosenBuildCoordinates = new Vector3 (0, 0, 0);
private string PlotSaleShield;
private GameObject PlotShield;
private GameObject PlotOwnerN;
public int Plot_Owner;
public GameObject PlotID;
// Start is called before the first frame update
void Start()
{
Controller = GameObject.Find("Controller");
//PlotID = GameObject.Find("PlotObjectName");
}
// Update is called once per frame
void Update()
{
}
public void Build_Bakery()
{
Debug.Log("HIER IST VOID FUNCTION");
PlotObjectName = Controller.GetComponent<IdController>().ChosenObjName; //get chosen build plot from raycast script
PlotToBuild = GameObject.Find("PlotObjectName");
Debug.Log(PlotObjectName);
ChosenBuildCoordinates = Controller.GetComponent<IdController>().ChosenObjCoordinates; //get coordinates from build plot
Debug.Log("Baukoordinaten sind: " + ChosenBuildCoordinates);
GameObject TestCube = Instantiate(Resources.Load("PrefabCube"), Vector3.zero, Quaternion.identity) as GameObject; //set the prefab model
TestCube.transform.position = ChosenBuildCoordinates;
PlotSaleShield = PlotObjectName + "_Shield"; //Create right VAR name including the "_Shield"
PlotShield = GameObject.Find(PlotSaleShield);
Debug.Log(PlotSaleShield);
PlotShield.SetActive(false);
PlotOwnerN = GameObject.Find("Canvas_PlotOwnerN"); //Close Buy Plot Menue
PlotOwnerN.SetActive(false);
//PlotID = GameObject.Find("PlotObjectName");
//PlotID.GetComponent<PlotController_ID>().Plot_Owner = 1; //Plot now depends to Player (1)
}
}
ScriptB (script that is attached to my building plot and contains “all” informations about it):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlotController_ID : MonoBehaviour
{
public int Plot_Owner = 0; //Vorerst als Int -> genügt für Singleplayer
//0 = Verwaltet durch die Stadt; 1 = Spieler; 2...3...n = KI bzw. Gegner
public int Plot_HouseType = 0; //Gebäudetyp
//0 = Freifläche; ... ..... ...... .......
public int Plot_ForSale = 1; //Grundstück kaufbar?
//0 = nicht kaufbar; 1 = kaufbar
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
As you can see i commended the code line out that should access the var in scriptB.
And I know the void Start command (line24) cant work because “PlotObjectName” isnt set yet.
Maybe anybody got another way to solve it, otherwise i have to redo it^^
Greetings
PS: I am not familiar with SerializeField thats why i use GetComponent overall.
You’re confusing GameObject.Find(“PlotObjectName”);
for
GameObject.Find(PlotObjectName);
if you want to look for an object that has a name that is the same as the value of your variable called PlotObjectName then don’t use quotation marks.
If you use quotation marks, then GameObject.Find will look for a game object that is named “PlotObjectName”. Do you have an object in your scene called “PlotObjectName”?