Reputation: 63
I have a small program that I am writing. I have a few small expressions that I am looking to access from another script. Problem being is that when I get the return in the inspector (which is correct), the other script doesn't recognize it. Upon my research, it seems that the void is not the correct return type on the function and I would need something else.
Also, since I am getting the correct return in the inspector, I thought it would be worth mentioning that I get a Null Ref error upon attempting to playing the game on the script that does not recognize the bool
I will be including the code that I am using. The version of Unity I am using is 5.2.1f1, 64-bit. Also, I have used Awake and Start. Neither have worked. As I didn't think that that would matter, but thought I would give it a shot.
I appreciate the help.
public class nonButtonStuff : MonoBehaviour
{
public float timer;
public float tipPercent;
public float checkAmount;
private Seated seated;
private dessert Dessert;
private drinks Drinks;
private food Food;
private appetisers Appetisers;
private checkedUpon checkedOn;
// Use this for initialization
void Awake()
{
tipPercent = .2f;
checkAmount = 0;
seated = GetComponent<Seated>();
Dessert = GetComponent<dessert>();
Drinks = GetComponent<drinks>();
Food = GetComponent<food>();
Appetisers = GetComponent<appetisers>();
checkedOn = GetComponent<checkedUpon>();
}
void Update()
{
if(seated.isSeated == true && Drinks.drinksOrdered == true || Drinks.drinksOrdered != false && seated.isSeated == false)
{
timer += Time.deltaTime * 1;
}
if(Dessert.Dessert==true)
{
timer = 0;
}
if (checkedOn.checkedOn > 0)
{
Debug.Log(checkedOn);
}
}
}
~~~~~~~~~~
public class Seated : MonoBehaviour
{
public bool isSeated = false;
public void clicked()
{
isSeated = true;
Debug.Log("Was clicked.");
return;
}
}
Edited for clarity about the clicked():
I used the built in UI editor to add a button for seated. So, there's no hard-coded button. But, when I "play" and click the button, it gives me the boolean as correct. However, nonButtonStuff doesn't recognize "isSeated" as == true.
Upvotes: 0
Views: 258
Reputation: 648
I don't think your problem is with isSeated. I think the problem is in the logic of this line:
if(seated.isSeated == true && Drinks.drinksOrdered == true || Drinks.drinksOrdered != false && seated.isSeated == false)
You're essentially saying "if isSeated and drinksOrdered or drinks ordered and not seated. That will always return true if drinks are ordered, regardless of the state of isSeated.
Also a note about style. In C# you don't need to compare boolean values using "==" like you're doing. This:
if(myBoolean){}
is the same code as
if(myBoolean == true){}
Most people like to write the simpler code so that there is less risk of logic errors. Your code can therefor be rewritten as
if(seated.isSeated && Drinks.drinksOrdered || Drinks.drinksOrdered && !seated.isSeated)
Since
if(seated.isSeated || !seated.isSeated)
well always return true, you can just remove those values. That leaves you with
if(Drinks.drinksOrdered || Drings.drinksOrdered)
That can be reduced to simply
if(Drinks.drinksOrdered)
This is the exact same logic as the original code:
if(seated.isSeated == true && Drinks.drinksOrdered == true || Drinks.drinksOrdered != false && seated.isSeated == false)
It is just clearer.
Upvotes: 1
Reputation: 2220
If you're getting a Null Reference Exception, that most likely means that your seated variable (set in the Awake function) is not being set to anything, so the call to GetComponent is failing to find the monobehaviour on the game object. This would happen if your Seated Monobehavior and your nonButtonStuff Monobehavior don't exist on the same GameObject, so be sure that they do.
Upvotes: 0