- Home /
Question by
beltmanmarc · Oct 02, 2016 at 05:40 AM ·
script error
can not access variable of prefab script
I have a script "conversation" to instantiate a prefab and a method "ImportTextFile" with an array as a return type and with a hard reference to a prefab "DialoguePrefab" . Before the prefab is instantiated I want to approach the prefab script "Dialogue" to set the variable "public DialogueStrings" equal to the returntype of my method from the "conversation" script. i thought "DialoguePrefab.GetComponent().DialogueStrings = ImportTextFile("testB");" would do the trick but I get the following error: Object reference not set to an instance of an object.. What am I doing wrong?
public class conversation : MonoBehaviour
{
public GameObject DialoguePrefab;
public Transform DialogueHolder;
public float DialogueOffsetX = 5f;
public float DialogueOffsetY = -0.75f;
private GameObject DialoguePrefabClone;
private GameObject canvas;
private bool isHit;
void Start()
{
isHit = false;
canvas = GameObject.Find("Canvas");
//DialoguePrefab.GetComponent<Dialogue>().DialogueStrings = ImportTextFile("testB");
}
void OnTriggerEnter(Collider other)
{
if (other.name == "Player" && !isHit)
{
DialoguePrefabClone = Instantiate(DialoguePrefab);
Vector2 screenPosition = Camera.main.WorldToScreenPoint(new Vector2(DialogueHolder.position.x + DialogueOffsetX, DialogueHolder.position.y + DialogueOffsetY));
DialoguePrefabClone.transform.SetParent(canvas.transform, false);
DialoguePrefabClone.transform.position = screenPosition;
isHit = true;
}
}
public string[] ImportTextFile(string textFile)
{
string[] DialogueStrings = new string[10];
TextAsset TextFileToLoad = (TextAsset)Resources.Load(textFile);
if (TextFileToLoad != null)
{
DialogueStrings = new string[1];
DialogueStrings = (TextFileToLoad.text.Split('\n'));
}
return DialogueStrings;
}
}
Comment
Your answer
