- Home /
'time' not a member of 'Object'
I got this from somewhere and I really like it for light trails, but it doesn't play well with #pragma strict. I know in a lot of similar questions that they recommend using List, but I can't use System.Collections.Generic because I don't think it's compatible with iphone/andoid etc. With #pragma strict I get:
'time' not a member of 'Object' and 'point' not a member of 'Object'
Could someone walk me through how to fix this? I'm a little lost, it has to do with dynamic typecasting, right? Thanks a lot.
#pragma strict
/*
Generates a trail that is always facing upwards using the scriptable mesh interface.
vertex colors and uv's are generated similar to the builtin Trail Renderer.
To use it
1. create an empty game object
2. attach this script and a MeshRenderer
3. Then assign a particle material to the mesh renderer
*/
var height = 2.0;
var time = 2.0;
var alwaysUp = false;
var minDistance = 0.1;
var startColor = Color.white;
var endColor = Color (1, 1, 1, 0);
class TronTrailSection
{
var point : Vector3;
var upDir : Vector3;
var time : float;
}
private var sections = new Array();
function LateUpdate () {
var position = transform.position;
var now = Time.time;
// Remove old sections
while (sections.length > 0 && now > sections[sections.length - 1].time + time) {
sections.Pop();
}
// Add a new trail section
if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
{
var section = TronTrailSection ();
section.point = position;
if (alwaysUp)
section.upDir = Vector3.up;
else
section.upDir = transform.TransformDirection(Vector3.up);
section.time = now;
sections.Unshift(section);
}
// Rebuild the mesh
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();
// We need at least 2 sections to create the line
if (sections.length < 2)
return;
var vertices = new Vector3[sections.length * 2];
var colors = new Color[sections.length * 2];
var uv = new Vector2[sections.length * 2];
var previousSection : TronTrailSection = sections[0];
var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons
var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex, uv and colors
for (var i=0;i<sections.length;i++)
{
previousSection = currentSection;
currentSection = sections[i];
// Calculate u for texture uv and color interpolation
var u = 0.0;
if (i != 0)
u = Mathf.Clamp01 ((Time.time - currentSection.time) / time);
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
uv[i * 2 + 0] = Vector2(u, 0);
uv[i * 2 + 1] = Vector2(u, 1);
// fade colors out over time
var interpolatedColor = Color.Lerp(startColor, endColor, u);
colors[i * 2 + 0] = interpolatedColor;
colors[i * 2 + 1] = interpolatedColor;
}
// Generate triangles indices
var triangles = new int[(sections.length - 1) * 2 * 3];
for (i=0;i<triangles.length / 6;i++)
{
triangles[i * 6 + 0] = i * 2;
triangles[i * 6 + 1] = i * 2 + 1;
triangles[i * 6 + 2] = i * 2 + 2;
triangles[i * 6 + 3] = i * 2 + 2;
triangles[i * 6 + 4] = i * 2 + 1;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
}
" but I can't use System.Collections.Generic because I don't think it's compatible with iphone/andoid etc." Come on now; that hasn't been the case for years. It's ridiculous to have gone with that belief ins$$anonymous$$d of finding out for sure.
Oh really? I assumed that was the case. I read about someone having trouble with it on iphone and didn't want to risk running into it. Well, I tried doing this with List. I replaced .length with .Count. Now I have "'Pop' and 'Unshift' is not a member of System.Collections.Generic.List[of TronTrailSelection]". What do I do about that?
Study these: http://unity3d.com/support/documentation/ScriptReference/Array.html http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx You never should use the former, but for a case like this, you need to know how to translate to a better class.
This helped me a LOT when I started working with Unity) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F
Thanks for all the suggetions. It's a lot for now but I'm looking over it all. I tried using sections.Add(section) for sections.Unshift(section) and sections.Clear() for sections.Pop(). It sort of works but the beginning of my trail doesn't disappear and stays visible. I'm getting a little lost, this is what happens when I try to modify something I didn't make. Any ideas?
Answer by olivier etienne · Oct 07, 2013 at 01:15 AM
Ran into the same problem and found a solution ()
sections.Pop();
is to be replaced by the following 3 lines:
var sectionsarr = new Array (sections);
sectionsarr.Pop();
sections = sectionsarr.ToBuiltin(TronTrailSection);
and sections.Unshift(section);
is to be replaced by the following 3 lines:
sectionsarr = new Array (sections);
sectionsarr.Unshift(section);
sections = sectionsarr.ToBuiltin(TronTrailSection);
the rest of the code remain the same, and it works like charm!
Please see the full modified source below and let me if it works for you.
#pragma strict
/*
Generates a trail that is always facing upwards using the scriptable mesh interface.
vertex colors and uv's are generated similar to the builtin Trail Renderer.
To use it
1. create an empty game object
2. attach this script and a MeshRenderer
3. Then assign a particle material to the mesh renderer
*/
var height = 2.0;
var time = 2.0;
var alwaysUp = false;
var minDistance = 0.1;
var startColor = Color.white;
var endColor = Color (1, 1, 1, 0);
class TronTrailSection
{
var point : Vector3;
var upDir : Vector3;
var time : float;
}
//private var sections = new Array();
private var sections = new TronTrailSection[0];
function LateUpdate () {
var position = transform.position;
var now = Time.time;
// Remove old sections
while (sections.length > 0 && now > sections[sections.length - 1].time + time) {
var sectionsarr = new Array (sections);
sectionsarr.Pop();
sections = sectionsarr.ToBuiltin(TronTrailSection);
//sections.Pop();
}
// Add a new trail section
if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
{
var section = TronTrailSection ();
section.point = position;
if (alwaysUp)
section.upDir = Vector3.forward;
else
section.upDir = transform.TransformDirection(Vector3.forward);
section.time = now;
sectionsarr = new Array (sections);
sectionsarr.Unshift(section);
sections = sectionsarr.ToBuiltin(TronTrailSection);
//sections.Unshift(section);
}
// Rebuild the mesh
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();
// We need at least 2 sections to create the line
if (sections.length < 2)
return;
var vertices = new Vector3[sections.length * 2];
var colors = new Color[sections.length * 2];
var uv = new Vector2[sections.length * 2];
var previousSection : TronTrailSection = sections[0];
var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons
var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex, uv and colors
for (var i=0;i<sections.length;i++)
{
previousSection = currentSection;
currentSection = sections[i];
// Calculate u for texture uv and color interpolation
var u = 0.0;
if (i != 0)
u = Mathf.Clamp01 ((Time.time - currentSection.time) / time);
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
uv[i * 2 + 0] = Vector2(u, 0);
uv[i * 2 + 1] = Vector2(u, 1);
// fade colors out over time
var interpolatedColor = Color.Lerp(startColor, endColor, u);
colors[i * 2 + 0] = interpolatedColor;
colors[i * 2 + 1] = interpolatedColor;
}
// Generate triangles indices
var triangles = new int[(sections.length - 1) * 2 * 3];
for (i=0;i<triangles.length / 6;i++)
{
triangles[i * 6 + 0] = i * 2;
triangles[i * 6 + 1] = i * 2 + 1;
triangles[i * 6 + 2] = i * 2 + 2;
triangles[i * 6 + 3] = i * 2 + 2;
triangles[i * 6 + 4] = i * 2 + 1;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
}
@script RequireComponent (MeshFilter)
Forgot to mention that, at the beginning of the script:
private var sections = new Array();
is to be replace by
private var sections = new TronTrailSection[0];
(Sorry for my english))Thanks! It works great, but trail moves with some angle.
if (alwaysUp)
section.upDir = Vector3.forward; else section.upDir = transform.TransformDirection(Vector3.forward);
So I paste "Vector3.up" ins$$anonymous$$d "Vector3.forward".