My sentry move script will not work it keeps coming up with cannot convert int too bool? using UnityEngine; using System.Collections; public class moverguy : MonoBehaviour { float r = Random.Range(1f,4f); int g = 0; void Update () { r = g; if (g = 1) transform.Translate(Vector3.left * 10 * Time.deltaTime); if(g = 2) transform.Translate(Vector3.right * 10 * Time.deltaTime); if(g = 3) transform.Translate(Vector3.back * 10 * Time.deltaTime); if(g = 4) transform.Translate(Vector3.forward * 10 * Time.deltaTime); } } help
you assign an integer value (fe 1) to a variable (g) and then you test g which is an int. you probably want to use the equality operator (==). and please use code tags.
Thanks the errors stopped but the script will no function. Its supposed to move random directions any help?
g is never 1, 2, 3, or 4. You assign r a random value and then immediately in Update assign it to g which is 0. You also never use r for anything.
Because r is 0 because you assign it the value from g in Update. Try using some Debug.Log statements in your code to see what the values of things are.
I tried enums but still didn't work. It does nothing but no errors. It should make the object move in 1 of 4 directions repeatedly. Here's the script. enum Mover {left, right, forward, back};void Start () { } Mover ReverseMover (Mover mov) { if(mov == Mover.left) transform.Translate(Vector3.left * 10 * Time.deltaTime); if(mov == Mover.right) transform.Translate(Vector3.right * 10 * Time.deltaTime); if(mov == Mover.back) transform.Translate(Vector3.back * 10 * Time.deltaTime); if(mov == Mover.forward) transform.Translate(Vector3.forward * 10 * Time.deltaTime); return mov; }
First, please use code tags. You don't need to use enums (you certainly could, but it's a bit overkill for something like this) Code (csharp): int direction=0; void Start () { direction = Random.Range (1, 5); // Random.Range (int, int) is [inclusive, exclusive) so it will choose a number between 1-4 } void Update () { if (direction == 1) transform.Translate(Vector3.left * 10 * Time.deltaTime); else if (direction == 2) transform.Translate(Vector3.right * 10 * Time.deltaTime); else if (direction == 3) transform.Translate(Vector3.back * 10 * Time.deltaTime); else if (direction == 4) transform.Translate(Vector3.forward * 10 * Time.deltaTime); } Now, here's the same functionality but with some optimizations. Code (csharp): Vector3 direction=Vector3.zero; Transform trans; // Cached Transform so we don't have to find the transform every frame void Start () { trans = this.transform; // Pre-load the direction, so we don't have to figure out which direction we're moving every frame int rand = Random.Range (1, 5); if (rand == 1) { direction = Vector3.left; } else if (rand == 2) { direction = Vector3.right; } else if (rand == 3) { direction = Vector3.back; } else if (rand == 4) { direction = Vector3.forward; } } void Update () { trans.Translate (direction*10*Time.deltaTime); }