Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Observing the direction of an gameobject

Discussion in 'Scripting' started by billy2k1, Dec 25, 2022.

  1. billy2k1

    billy2k1

    Joined:
    Nov 11, 2021
    Posts:
    9
    Hi, I am struggling on understanding on how transform works.

    There are multiple transform(cars) stored in a list, with another list tracking each transform checkpoint count.

    Below is a describing a method that gathers the nextcheckpoint of the current transform, and attempts to gather the forward direction.

    Code (CSharp):
    1.     public override void CollectObservations(VectorSensor sensor)
    2.     {
    3.         Vector3 checkpointForward = trackCheckpoints.GetNextCheckpoint(transform).transform.forward;
    4.         float directionDot = Vector3.Dot(transform.forward, checkpointForward);
    5.         sensor.AddObservation(directionDot);
    6.     }
    Code (CSharp):
    1.     public void GetNextCheckpoint(Transform carTransform)
    2.     {
    3.         int nextCheckpointSingleIndex = nextCheckpointSingleIndexList[carTransformList.IndexOf(carTransform)];
    4.  
    5.     }
    With this implementation I get
    Operator '.' cannot be applied to operand of type 'void'

    If someone could lead me to the right direction that would be appreciated :)
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    The error message should tell you which code file the error is in. After the error message there are two numbers in parentheses. The first number is the line of code where the error is supposed to be, the second number is the exact character. Once you find that it should be pretty easy to spot the error.

    I think maybe you are attempting to access a return value of a method that does not return a value.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Specifically, you're doing
    Code (csharp):
    1. GetNextCheckpoint(transform).transform ...
    When clearly GetNextCheckpoint returns void (in the next codeblock) and you can't access transform from void. Void is nothing.
     
    AnimalMan likes this.
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    So the function method does not have a return because it is a void. Does not return. But you ask for something to = a function that does not return.