Search Unity

Passing variables from a class to a function (Error CS0103)

Discussion in 'Scripting' started by Chris10011, Jul 11, 2019.

  1. Chris10011

    Chris10011

    Joined:
    Jun 24, 2019
    Posts:
    5
    I'm following a tutorial on how to make tank tracks, so the code is not mine. Unfortunately, the code they provided is awfully formatted. As well as this it's quite dated, however, that's neither here nor there. Just some context.

    If you're curious, here's the tutorial translated from Russian to English: http://translate.google.com/transla.../habrahabr.ru/post/116088/&edit-text=&act=url

    It should also be noted I am new to object oriented programming.

    So here is the problem: The function UpdateWheels (as shown in the code) needs to make use of the contents of the class WheelData, however it cannot access them as they do not exist in the current context of the function UpdateWheels (Error CS0103).

    I will now post the code that is relevant to the problem.

    Code (CSharp):
    1.     public class WheelData
    2.     {
    3.         public Transform wheelTransform;
    4.         public Transform boneTransform;
    5.         public WheelCollider col;
    6.         public Vector3 wheelStartPos;
    7.         public Vector3 boneStartPos;
    8.         public float rotation = 0.0f;
    9.         public Quaternion startWheelAngle;
    10.         protected WheelData[] leftTrackWheelData;
    11.         protected WheelData[] rightTrackWheelData;
    12.         protected float leftTrackTextureOffset = 0.0f;
    13.         protected float rightTrackTextureOffset = 0.0f;
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         UpdateWheels(); //1
    19.     }
    20.  
    21.   public void UpdateWheels()
    22.     { //1
    23.         float delta = Time.fixedDeltaTime; //2
    24.         float trackRpm = CalculateSmoothRpm(leftTrackWheelData); //3
    25.         foreach (WheelData w in leftTrackWheelData)
    26.         { //4
    27.             w.wheelTransform.localPosition = CalculateWheelPosition(w.wheelTransform,w.col,w.wheelStartPos); //5
    28.             w.boneTransform.localPosition = CalculateWheelPosition(w.boneTransform,w.col,w.boneStartPos); //6
    29.             w.rotation = Mathf.Repeat(w.rotation + delta * trackRpm * 360.0f / 60.0f, 360.0f); //7
    30.             w.wheelTransform.localRotation = Quaternion.Euler(w.rotation, w.startWheelAngle.y, w.startWheelAngle.z); //8
    31.         }
    32.         leftTrackTextureOffset = Mathf.Repeat(leftTrackTextureOffset + delta*trackRpm*trackTextureSpeed/60.0f,1.0f); //9
    33.         leftTrack.GetComponent<Renderer>().material.SetTextureOffset("_MainTex",new Vector2(0,-leftTrackTextureOffset)); //10
    34.         trackRpm = CalculateSmoothRpm(rightTrackWheelData); //3
    35.         foreach (WheelData w in rightTrackWheelData)
    36.         { //4
    37.         w.wheelTransform.localPosition = CalculateWheelPosition(w.wheelTransform,w.col,w.wheelStartPos); //5
    38.         w.boneTransform.localPosition = CalculateWheelPosition(w.boneTransform,w.col,w.boneStartPos); //6
    39.         w.rotation = Mathf.Repeat(w.rotation + delta * trackRpm * 360.0f / 60.0f, 360.0f); //7
    40.         w.wheelTransform.localRotation = Quaternion.Euler(w.rotation, w.startWheelAngle.y, w.startWheelAngle.z); //8
    41.         }
    42.         rightTrackTextureOffset = Mathf.Repeat(rightTrackTextureOffset + delta*trackRpm*trackTextureSpeed/60.0f,1.0f); ///9
    43.         rightTrack.GetComponent<Renderer>().material.SetTextureOffset("_MainTex",new Vector2(0,-rightTrackTextureOffset)); //10
    44.         for (int i=0;i<leftTrackUpperWheels.Length;i++)
    45.         { //11
    46.             leftTrackUpperWheels[i].localRotation = Quaternion.Euler(leftTrackWheelData[0].rotation, leftTrackWheelData[0].startWheelAngle.y, leftTrackWheelData[0].startWheelAngle.z); //11
    47.         }
    48.         for (int i=0;i<rightTrackUpperWheels.Length;i++)
    49.         { //11
    50.             rightTrackUpperWheels[i].localRotation = Quaternion.Euler(rightTrackWheelData[0].rotation, rightTrackWheelData[0].startWheelAngle.y, rightTrackWheelData[0].startWheelAngle.z); //11
    51.         }
    52.     }
    53.     private float CalculateSmoothRpm(WheelData[] w)
    54.     { //12
    55.         float rpm = 0.0f; List<int> grWheelsInd = new List<int>(); //13
    56.         for (int i = 0;i<w.Length;i++)
    57.         { //14
    58.             if (w[i].col.isGrounded)
    59.             { //14
    60.                 grWheelsInd.Add(i); //14
    61.             }
    62.         }
    63.         if (grWheelsInd.Count == 0)
    64.         { //15
    65.             foreach (WheelData wd in w)
    66.             { //15
    67.                 rpm +=wd.col.rpm; //15
    68.             }
    69.             rpm /= w.Length; //15
    70.         }
    71.         else
    72.         { //16
    73.             for (int i = 0;i<grWheelsInd.Count;i++)
    74.             { //16
    75.                 rpm +=w[grWheelsInd[i]].col.rpm; //16
    76.             } rpm /= grWheelsInd.Count; //16
    77.         }
    78.         return rpm; //17
    79.     }
    80.     private Vector3 CalculateWheelPosition(Transform w,WheelCollider col,Vector3 startPos)
    81.     { //18
    82.         WheelHit hit; Vector3 lp = w.localPosition; if (col.GetGroundHit(out hit)) { lp.y -= Vector3.Dot(w.position - hit.point, transform.up) - wheelRadius; }else { lp.y = startPos.y - suspensionOffset; } return lp; }
    83.  
    84.     }
    85.  
    The error: So when UpdateWheels is trying to access variables from the class WheelData, they come up with CS0103 errors (name does not exist in current context)
     
  2. bgoldbeck

    bgoldbeck

    Joined:
    May 15, 2019
    Posts:
    6
    Are you doing the correct using statements? using UnityEngine; etc...
    Also, if you are using FixedUpdate(), this class should be a monobehavior.
    public class WheelData : MonoBehaviour
     
    Chris10011 likes this.
  3. Chris10011

    Chris10011

    Joined:
    Jun 24, 2019
    Posts:
    5
    Yeah I should've included this context sorry.
    WheelData is a class within the script TankTrackController:
    public class TankTrackController : MonoBehaviour

    and the statements at the top are...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;