Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Uncaught TypeError: Cannot read property 'offset' of undefined

Discussion in 'Project Tiny' started by vincismurf, Dec 12, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Attached is a screen shot of my component,

    Here is the code

    Code (JavaScript):
    1.  
    2. export class CamerFollowFilter extends ut.EntityFilter {
    3.         node: ut.Core2D.TransformNode;
    4.         position?: ut.Core2D.TransformLocalPosition;
    5.         rotation?: ut.Core2D.TransformLocalRotation;
    6.         scale?: ut.Core2D.TransformLocalScale;
    7.         follow?:game.CameraFollow;
    8.     }
    9.  
    10.     export class CamerFollowBehavior extends ut.ComponentBehaviour {
    11.  
    12.         data: CamerFollowFilter;
    13.  
    14.         intermediatePosition:Vector3;
    15.         // ComponentBehaviour lifecycle events
    16.         // uncomment any method you need
    17.    
    18.         // this method is called for each entity matching the NewBehaviourFilter signature, once when enabled
    19.         OnEntityEnable():void
    20.         {
    21.        
    22.             this.world.forEach([game.MovementData, ut.Core2D.TransformLocalPosition],(player, position)=>
    23.                 {
    24.                     let cameraPosition = new Vector3(this.data.position.position.x,this.data.position.position.y,this.data.position.position.z);
    25.                     let targetPosition = new Vector3(position.position.x,position.position.y,position.position.z);
    26.                     console.log("this.data.follow.offset " + this.data.follow.offset.x +  " " + this.data.follow.offset.y + " " + this.data.follow.offset.z);
    27.                     //console.log("targetPosition " + targetPosition.x +  " " + targetPosition.y + " " + targetPosition.z);
    28.                     this.data.follow.offset = cameraPosition.sub(targetPosition);
    29.                     let offsetV = new Vector3(this.data.follow.offset.x,this.data.follow.offset.y,this.data.follow.offset.z);
    30.                     this.intermediatePosition = targetPosition.add(offsetV);
    31.                 });
    32.          }
    33. }
    And here is the Error ( It crashes at the first console log)

    tsc-emit.js:37 Uncaught TypeError: Cannot read property 'offset' of undefined
    at tsc-emit.js:37
    at World.forEach (runtime.js:127554)
    at CamerFollow.OnEntityEnable (tsc-emit.js:34)
    at bindings.js:1702
    at main.js:52
    at World.forEach (runtime.js:127554)
    at CamerFollowFilter.game.CamerFollowFilter.ForEach (main.js:50)
    at Object.<anonymous> (bindings.js:1691)
    at Array.<anonymous> (runtime.js:976)
    at _emscripten_asm_const_iiii (runtime.js:995)


    Also a Follow up. . . . can I get the TransformPositionComponents from the Entity Referance on the CameraFollow Componenet? ( IE avoiding the this.world.foreach() call?)
     

    Attached Files:

    Last edited: Dec 12, 2018
  2. etienne_unity

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    102
    Try removing the
    ?
    optional modifier from the
    follow
    member.
     
  3. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I tried that, here is what happened
    Code (JavaScript):
    1. export class CamerFollowFilter extends ut.EntityFilter {
    2.         node: ut.Core2D.TransformNode;
    3.         follow:game.CameraFollow;
    4.         position?: ut.Core2D.TransformLocalPosition;
    5.         rotation?: ut.Core2D.TransformLocalRotation;
    6.         scale?: ut.Core2D.TransformLocalScale;
    7.        
    8.     }
    Error

    Uncaught TypeError: Cannot read property 'position' of undefined
    at tsc-emit.js:35
    at World.forEach (runtime.js:127554)
    at CamerFollow.OnEntityEnable (tsc-emit.js:34)
    at bindings.js:1702
    at main.js:52
    at World.forEach (runtime.js:127560)
    at CamerFollowFilter.game.CamerFollowFilter.ForEach (main.js:50)
    at Object.<anonymous> (bindings.js:1691)
    at Array.<anonymous> (runtime.js:976)
    at _emscripten_asm_const_iiii (runtime.js:995)
     
  4. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Follow up . . . . I had to remove optional from position as well! Now it is working. . . . I was wondering what the ? meant seems like it was the culpret
     
  5. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Correction providing this filter

    Code (JavaScript):
    1.     export class CamerFollowFilter extends ut.EntityFilter {
    2.         node: ut.Core2D.TransformNode;
    3.         follow:game.CameraFollow;
    4.         position: ut.Core2D.TransformLocalPosition;
    5.         rotation?: ut.Core2D.TransformLocalRotation;
    6.         scale?: ut.Core2D.TransformLocalScale;
    7.      
    8.     }
    Seems to cause the class to be ignored entirely. The console logs don't fire anything
     
    Last edited: Dec 12, 2018
  6. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I found the problem. . . . I assumed the Transform family on the Generated camera had a
    TransformLocalPosition already on it. . . .it did not. Live and learn