-1
\$\begingroup\$

This is my scenario:

I have a 3D model of a house, and an autonomously moving robot moving through the house picking up multiple objects. I have an array of 3 objects I want it to pick up, but I don't know how to make it move from item 1 (goals[0]) to item 2 (goals[1]) to item 3 (goals[2]). I do have it from item 1 to 2, but I don't know how to update the i value. It gives me an error and I don't know what to do.

This is my code:

using UnityEngine;
using System.Collections;

public class MoveTo : MonoBehaviour {

public Transform[] goals;

private bool printDone;
int i = 0;

void Start () {
    NavMeshAgent agent = GetComponent<NavMeshAgent> ();
    agent.destination = goals[i].position;
    printDone = false;

}

void Update () {

    NavMeshAgent agent = GetComponent<NavMeshAgent> ();
    if (agent.remainingDistance < 2 && !printDone) {        

        print ("Found an item: " + goals [i].name + ". This is a " + goals [i].tag);
        printDone = true;
        agent.destination = goals [i + 1].position;
        int i = i + 1;
    }
}
}

the int i = i + 1 gives the bug: Assets/MoveTo.cs(26,29): error CS0136: A local variable named i' cannot be declared in this scope because it would give a different meaning toi', which is already used in a `parent or current' scope to denote something else

Anyone knows how I can solve this and get what I want?

Thank you kindly!

\$\endgroup\$
2
  • \$\begingroup\$ When you have questions which are not about Unity but rather about standard features of the C# programming language (like variable assignment), you should ask them on stackoverflow.com. Game development stackexchange is only for programming questions when they are about problems unique to game development and not relevant to other software products. \$\endgroup\$
    – Philipp
    Commented May 30, 2016 at 13:14
  • \$\begingroup\$ Did you read the error? \$\endgroup\$
    – Vaillancourt
    Commented May 30, 2016 at 15:07

1 Answer 1

2
\$\begingroup\$

Its because you have declared int i two times. If you want to save new value change:

int i = i + 1;

to:

i = i + 1;
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .