I have a prefab that I am using with a script attached to it. Here is the code:
using UnityEngine;
using System.Collections;
public class bagDetect : MonoBehaviour {
public GameObject okToShoot;
public bool canScore = true;
void Start () {
okToShoot.GetComponent<ID355> ().okToScore = true;
}
void Update () {
canScore = true;
}
}
the problem is i cannot seem to attach the reference to it. It will not let me drag and drop or assign the variable. This is the error i get when i run the game. Any ideas on what i am doing wrong???
UnassignedReferenceException: The variable okToShoot of bagDetect has not been assigned.
You probably need to assign the okToShoot variable of the bagDetect script in the inspector.
UnityEngine.GameObject.GetComponent[ID355] ()
bagDetect.Start () (at Assets/bagDetect.cs:8)
You may need to edit the prefab instance, rather than the prefab itself. The prefab's template can only contain references to assets or other objects internal to the prefab. You can only reference other scene objects once the prefab exists in a scene.
– rutterhow would i go about doing that?
– BaldiWonKenobii think the issue is that the prefab is something that gets instantiated and there is none on the board until an event happens
– BaldiWonKenobiso is it possible to assign a variable to prefab before it is instantiated?
– BaldiWonKenobiThe prefab itself can't hold references to scene objects, because there's no clear way to handle that if the prefab is dropped into another scene. If you need a reference to something in the scene, I'd recommend the following: * Drop a copy of the prefab into your scene, somewhere out of the way. * Select the instance and disable it (checkbox in the inspector, next to the GameObject's name). * Set whatever value you need to set. Your
– rutterInstantiatecall can target the new instance that you just configured.