- Home /
The variable has not been assigned - but it has
What I'm trying to do is for a 'buy button' to show when I click on an object i want to buy. The script does detect the buy button because I made it so that at start it sets it to be not active - and it does work.
.
.
The problem I am having is that I get an error saying "The variable 'buyButton' of 'Colors' has not been assigned" when I try to set the button to be active with a public function using another button. It points me to a public GameObject variable that I have actually assigned in the inspector. I also tried assigning it with GameObject.FindWithTag but that made no difference
.
.
I'm assuming it's because it's accessing it from a public function and it somehow looses the reference to that object? I just don't know how and what to do to make it not do that. Any help would be appreciated.
Are you sure you have a reference to the actual button and not a prefab or something?
@JedBeryll yes I've referenced the actual button from inside the canvas. I can manipulate the button from the same script with no issues in other non-public functions. The problem occurs when I try to access it from a public function using another button.
Answer by Xarbrough · Mar 20, 2018 at 12:49 PM
If the errors says "has not been assigned" you can be sure, that indeed some reference has not been assigned in the inspector. The most likely cause of your issue is, that you've assigned some variable, but there's another object with the same script attached, where you didn't assign it. To find the culprit you can log a debug line to the console and send the current instance as a context with the log statement. This way, when you click (or double-click) the log statement in the console, Unity will highlight the respective GameObject.
public Transform someVariable;
void Start()
{
if(someVariable == null)
Debug.LogError("SomeVariable has not been assigned.", this) ;
// Notice, that we pass 'this' as a context object so that Unity will highlight this object when clicked.
}
Additionally, make sure that you are actually modifying only the object you intend to, not a prefab or some duplicate in the scene. To make sure, I would create a new scene and place (or recreate) only the relevant objects for testing.
Assigning with GameObject.FindWithTag is not required and I wouldn't recommend it here. Just find the missing reference on one of your objects and set it manually.
I don't think the problem has a connection to any public function and Unity doesn't lose references in a way that matches what you describe. The only way for Unity to lose a reference would be if the referenced object would be destroyed or the field was set to null by another script. However, if any of this happened, Unity would log an error saying "missing reference exception, the object has been destroyed" or simply "null reference exception, the object is null". Since the error was "has not been assigned" we can be sure, that Unity has already noticed that nobody set the variable at runtime, instead it was null from the beginning.
Good luck! If nothing helps, create a small UnityPackage showing your problem and somebody here can take a look. ;)
@Xarbrough AHA! Thanks to your suggestion, I managed to isolate the problem by creating a clean project.
.
It turns out that it was because I'm instantiating the shopItemButton that shows the buyButton on click and because of that I had to create a prefab of the object that has my main script and the reference to buyButton so that I can access the public function on the many shopItemButton buttons I'm instantiating and that's where it looses the reference because the buyButton is not a prefab, it's in the scene.
.
To solve this I've made the buyButton a prefab to keep the reference to it on the main object prefab, but now I have to instantiate it ins$$anonymous$$d of setting it active to actually put it into the scene so it's visible. I'd rather not use instantiate here but I can't think of a different/better way - if YOU can I would very much appreciate you sharing it with me. Anyways, thank you for your help and time, you helped me alot!
You don't have to manually instantiate your buy button. You just have to assign the reference manually when you instantiate your shopItemButton. By "manually" i mean assigning it via code right after you instantiate the item.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Jumping on moving platforms problem c# 1 Answer
Distribute terrain in zones 3 Answers
Missing function in button on click 1 Answer