- Home /
enabling trail renderer
I am putting skidmarks in my game with a trail renderer that activates when the tire slips. I have this so far:
// Define the variables used in the script, the Corresponding collider is the wheel collider at the position of // the visible wheel, the slip prefab is the prefab instantiated when the wheels slide, the rotation value is the // value used to rotate the wheel around it's axel. var CorrespondingCollider : WheelCollider; var SlipPrefab : GameObject; private var RotationValue : float = 0.0;
function Update () {
// define a hit point for the raycast collision
var hit : RaycastHit;
// Find the collider's center point, you need to do this because the center of the collider might not actually be
// the real position if the transform's off.
var ColliderCenterPoint : Vector3 = CorrespondingCollider.transform.TransformPoint( CorrespondingCollider.center );
// now cast a ray out from the wheel collider's center the distance of the suspension, if it hit something, then use the "hit"
// variable's data to find where the wheel hit, if it didn't, then se tthe wheel to be fully extended along the suspension.
if ( Physics.Raycast( ColliderCenterPoint, -CorrespondingCollider.transform.up, hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius ) ) {
transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius);
}else{
transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance);
}
// now set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value
// is the rotation around the axle, and the rotation from steering input.
transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0 );
// increase the rotation value by the rotation speed (in degrees per second)
RotationValue += CorrespondingCollider.rpm * ( 360/60 ) * Time.deltaTime;
// define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine
// the slip of the tire.
var CorrespondingGroundHit : WheelHit;
CorrespondingCollider.GetGroundHit( CorrespondingGroundHit );
// if the slip of the tire is greater than 2.0, and the slip prefab exists, create an instance of it on the ground at
// a zero rotation.
if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > 2.0 ) {
if ( SlipPrefab ) {
Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );
}
}
if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > 2.0 ) {
TrailRenderer.enabled = true; //THIS IS THE PROBLEM
}
}
}
What i have here is working, apart from the last section with TrailRenderer. I get an error of
BCE0020: An instance type of 'UnityEngine.Renderer' is required to access non static member 'enabled'.
how do i fix this?
Answer by fherbst · Jul 11, 2010 at 02:11 PM
Use GetComponent(TrailRenderer).enabled = true
instead.
TrailRenderer
(uppercase) is the class name, but you want the particular instance on your gameObject. Many classes (like Rigidbody
) have direct access names (GameObject.rigidbody
), but TrailRenderer
hasn't, so you have to use GetComponent
to access it.
Thanks that works, but how would i get it so the trails stay there even when it's not active, or is that not possible?
Your answer
![](https://koobas.hobune.stream/wayback/20220613064410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
An instance of type X is required to access non static member Y [javascript] 4 Answers
An instance of type 'TurretControl' is required to access non static member 'Shoot'. 1 Answer
How would I make a route that shows where player moves beforehand? 0 Answers
what's wrong with this?? 2 Answers