Nick Greiner
Nick Greiner

Reputation: 21

Unity cannot implicitly convert type "string" to "bool"

So, I am trying to create a quiz, probably the hardest way possible knowing me, and this error keeps popping up (title of thread). This is the code, anyone know whats wrong?

using UnityEngine;
using System.Collections;

public class score : MonoBehaviour {

public string final_score;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    if (final_score = ("10"))
    {
        Application.LoadLevel("Result1");
    }

    if (final_score = ("20"))
    {
        Application.LoadLevel("Result2");
    }

    if (final_score = ("30"))
    {
        Application.LoadLevel("Result3");
    }

    if (final_score = ("40"))
    {
        Application.LoadLevel("Result4");
    }

    if (final_score = ("50"))
    {
        Application.LoadLevel("Result5");
    }

    if (final_score = ("60"))
    {
        Application.LoadLevel("Result6");
    }

  }

}

That's my main problem, but if at all possible, anyone know a good tutorial for a quiz app in unity? I am using the basic stuff to create it now, but I feel like there is a way easier way to do it and I don't know what that is. I found some tutorials, but none are what I need. My quiz has 8 questions, each with 4 answers. I need each answer to add up to determine a final result, which there are 6 of. I am using a scene for each question/result, should I change that? But if I did that how would I get the questions/answers to spawn in correctly? My main problem with the entire quiz is the results. How do I get the questions to add up through the different scenes to determine a result? Thanks! This project is on a schedule and needs to be done tomorrow, so fast results would be great! Please make sure the fixes/tutorials work on iOS and other mobile devices.

Upvotes: 0

Views: 1283

Answers (3)

meda
meda

Reputation: 45490

In addition to your comparison operator being wrong , score seems like it should be an integer.

You can also avoid the if statement, by using a dictionary , or a similar data structure.

Dictionary<int, string> quiz =
        new Dictionary<int, string>()
{
    { 10, "Result1" },
    { 20, "Result2" },
    { 30, "Result3" },
    { 40, "Result4" },
    { 50, "Result5" },
    { 60, "Result6" }
};

void Update (int score) {
    Application.LoadLevel(quiz[score]);
}

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

= operator is assignment operator, you need to use == operator which is equality operator.

Your

final_score = ("10")

line and other lines are executed as

  • assign "10" to your final_score variable
  • return this string object.

Since if statement expect boolean expression, and there is no implicit conversation between string and bool types, you get this error.

Upvotes: 3

Dharmesh Dhorajiya
Dharmesh Dhorajiya

Reputation: 3984

update you code = to == like this way because = operator is assignment.

void Update () {

if (final_score == ("10"))
{
    Application.LoadLevel("Result1");
}

if (final_score == ("20"))
{
    Application.LoadLevel("Result2");
}

if (final_score == ("30"))
{
    Application.LoadLevel("Result3");
}

if (final_score == ("40"))
{
    Application.LoadLevel("Result4");
}

if (final_score ==("50"))
{
    Application.LoadLevel("Result5");
}

if (final_score == ("60"))
{
    Application.LoadLevel("Result6");
}

  }

Upvotes: 2

Related Questions