Hello everyone,
I have looked for similar errors that other have been getting and I could not find one that was similar.
Here is my problem simplified…
I have two objects and two scripts. That need to interact with one another, for example, Object A is for scoring or something along that line. Object B will increment a variable on Object A.
Here is some simplified code from each script.
ScriptA
using UnityEngine;
using System.Collections;
public class ScriptA : MonoBehaviour {
public int x;
public GUIText xUI;
void Awake(){
x = 50;
}
void Update(){
xUI.text = "X = " + x.ToString ();
}
}
ScriptB
using UnityEngine;
using System.Collections;
public class ScriptB : MonoBehaviour {
public ScriptA otherScript;
public bool incrementX;
void Awake(){
incrementX = false;
}
void increment_X(){
otherScript.x += 50;
}
void Update (){
if (incrementX == true)
increment_X ();
}
}
Here is my question.
I know that I can easily just drag Object A into the location in Object B within the Inspector, however, Object B is going to be instantiated multiple times in random location around a map from it’s prefab location within the assets folder. I am trying to figure out how to get Object B to recognize Object A upon being spawned.
Some of the solutions that I have tried are using
GetComponents - within the Awake function - otherScript = GetComponent();
This did not work.
GameObject.Find - within the Awake function - otherScript = GameObject.Find (“Object A”);
This gave an error of - Cannot implicitly convert type UnityEngine.GameObject' to
ScriptA’ - which makes sense.
Could someone help me with a solution or point me at material that I could look over to hopefully fix this solution.
Thanks
Nic