Referencing an interface from another script
Hi, everyone. I'm still very green in C# and Unity, and I was hoping some of you could help me with an issue I ran into while making my first game.
There's 9 GameObjects that the player can click on. When one of them is clicked, Shoot(); is executed and it calls a Coroutine called Growth(); which then modifies its scale.
public void Shoot()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit) == GetComponent<TreeGrower>())
{
//Debug.Log(hit.transform.name);
StartCoroutine(myTreeGrower.Growth(15));
}
}
Now, Growth(); is in a second script (myTreeGrower), and I can't manage to call it from the 'main' script. The error that pops up is "Object reference not set to an instance of an object", which I believe means that the script is simply not attached to a GameObject, but I can't create a reference in-script so that I can then attach it in Unity's editor.
public IEnumerator Growth(float time)
{
Vector3 initialTreeScale = tree.transform.localScale;
Vector3 finalTreeScale = new Vector3(0.70f, 3f, 0.70f);
float currentTime = 0.0f;
do
{
tree.transform.localScale = Vector3.Lerp(initialTreeScale, finalTreeScale, currentTime / time);
currentTime += Time.deltaTime;
yield return null;
} while (currentTime <= time);
}
The second issue is that, when I had Growth(); in the same script (before I created the second one), I couldn't get it to be implemented in the object that was clicked. I could click any of the 9, but every time only one of them would be affected. Always the same one.
Is there a way to determine which object was clicked, and then modify that object's scale?
All things aside, if there's anything you see I can do better to improve my coding, please let me know.
Thank you all in advance!