How can I change a function from another script?
Hello everyone, I will give an example of what I mean: In class A: void IDoSmth(){ int smth = 5; } class B: if(smth){ //then it should access the func in classA and chang the int classA.IDOSmth.Change The Int To 10
} how can I do that
thank you
Answer by ADiSiN · Jul 14, 2019 at 01:10 AM
Hi!
If I understood you correctly then below you can see 2 examples to achieve that based on your aim.
This is our first class.
using UnityEngine;
public class ClassA : MonoBehaviour
{
int i_WhatWeChange = 0;
public int changeAmount = 5;
public void ChangeVar1()
{
i_WhatWeChange = changeAmount;
}
public void ChangeVar2(int value)
{
i_WhatWeChange = value;
}
}
That is our second class.
using UnityEngine;
public class ClassB : MonoBehaviour
{
public ClassA classA;
public void Option1()
{
classA.changeAmount = 10;
}
public void Option2()
{
classA.ChangeVar2(10);
}
}
So, in Option1() we just change the public variable of ClassA so the next time function ChangeVar1() will be called the changeAmount will be different from instantiated, however we do not call function ChangeVar1() here.
In Option2() we call function ChangeVar2(int value) and give it any value to apply, but we do not change any variable of ClassA.