#pragma strict problem

i am trying to port our game to android, and i am going through scripts and trying to
resolve issues with dynamic typing. i managed to fix most of the things but i can not figure out one of them.

i have these two variables:

var CylinderCollider : Transform; //this one is set in inspector (ColliderObject)
private var CylinderColliderScript : ColliderObject;

later in awake i get reference to CylinderColliderScript like this:

CylinderColliderScript=GameObject.Find("ColliderObject").GetComponent("ColliderObject") as ColliderObject;//i already fixed this so this is not an issue i guess

later when i want to set the height of the cylinder it gives me error
(it works in editor but for android build it complains)

CylinderCollider.collider.height=dist*2;

i guess it is problem with typing, can somebody help me with this?

thanks!

You could simplify your one line using generics to CylinderColliderScript = GameObject.Find("ColliderObject").GetComponent.<ColliderObject">(); As to your last line... where and what is dist defined? What is the precise error you're getting?

dist is defined just above like this: var dist : float=Vector3.Distance(camPosition,target.position); basically it sets the collider height to be half of the distance from camera to selected object. i know that CylinderCollider is declared as Transform, but it works in editor and other platforms except android if i want to get collider of that transform like this: CylinderCollider.collider.height any help appreciated!

And what is the error that you're getting?

i am getting this error: 'height' is not a member of 'UnityEngine.Collider'

Yea, that sounds right. Height isn't a member of Collider. (http://unity3d.com/support/documentation/ScriptReference/Collider.html). CylinderCollider is a transform, from which you're referencing a Collider. Do you mean to reference your ColliderObject instead?

1 Answer

1

For the sake of later readers:

var capsule = collider as CapsuleCollider;
capsule.height = ...;

or even:

(collider as CapsuleCollider).height = ...;

These are fully strict-compliant, do not require pragma downcast, and give full performance.