Search Unity

Helpc3 script not working

Discussion in 'Scripting' started by EarthenSky, Apr 3, 2014.

  1. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    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
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    g == 0 is comparison. g = 0 is assignment
     
  3. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    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.
     
  4. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    Thanks the errors stopped but the script will no function.

    Its supposed to move random directions any help?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  6. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    using r instead didn't help either any other predictions?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  8. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    ok ill see thanks fo the help
     
  9. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    still nowork
     
  10. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    What does your script look like now?

    What is it doing?

    What do you expect it to do?
     
  11. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    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;
    }
     
  12. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    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):
    1.  
    2. int direction=0;
    3.  
    4. void Start () {
    5.     direction = Random.Range (1, 5); // Random.Range (int, int) is [inclusive, exclusive) so it will choose a number between 1-4
    6. }
    7.  
    8. void Update () {
    9.     if (direction == 1)
    10.         transform.Translate(Vector3.left * 10 * Time.deltaTime);
    11.     else if (direction == 2)
    12.         transform.Translate(Vector3.right * 10 * Time.deltaTime);
    13.     else if (direction == 3)
    14.         transform.Translate(Vector3.back * 10 * Time.deltaTime);
    15.     else if (direction == 4)
    16.         transform.Translate(Vector3.forward * 10 * Time.deltaTime);
    17. }
    18.  
    Now, here's the same functionality but with some optimizations.
    Code (csharp):
    1.  
    2. Vector3 direction=Vector3.zero;
    3. Transform trans; // Cached Transform so we don't have to find the transform every frame
    4.  
    5. void Start () {
    6.     trans = this.transform;
    7.    
    8.     // Pre-load the direction, so we don't have to figure out which direction we're moving every frame
    9.     int rand = Random.Range (1, 5);
    10.     if (rand == 1) {
    11.         direction = Vector3.left;
    12.     } else if (rand == 2) {
    13.         direction = Vector3.right;
    14.     } else if (rand == 3) {
    15.         direction = Vector3.back;
    16.     } else if (rand == 4) {
    17.         direction = Vector3.forward;
    18.     }
    19.  
    20. }
    21.  
    22. void Update () {
    23.     trans.Translate (direction*10*Time.deltaTime);
    24. }
    25.