- Home /
Problem is not reproducible or outdated
How to add material to path follow?
Hi everyone.
I would like to add material to my path follow.
I found an interesting script that allows me to do it.
However, every time I get an error in the 24th line:
GetComponent (). SharedMaterial.mainTextureScale = new Vector2 (1, textureRepeat);
Does anyone know how to solve it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(PathCreator))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class RoadCreator : MonoBehaviour {
[Range(.05f, 1.5f)]
public float spacing = 1;
public float roadWidth = 1;
public bool autoUpdate;
public float tiling = 1;
public void UpdateRoad()
{
Path path = GetComponent<PathCreator>().path;
Vector2[] points = path.CalculateEvenlySpacedPoints(spacing);
GetComponent<MeshFilter>().mesh = CreateRoadMesh(points, path.IsClosed);
int textureRepeat = Mathf.RoundToInt(tiling * points.Length * spacing * .05f);
GetComponent<MeshRenderer>().sharedMaterial.mainTextureScale = new Vector2(1, textureRepeat);
}
Mesh CreateRoadMesh(Vector2[] points, bool isClosed)
{
Vector3[] verts = new Vector3[points.Length * 2];
Vector2[] uvs = new Vector2[verts.Length];
int numTris = 2 * (points.Length - 1) + ((isClosed) ? 2 : 0);
int[] tris = new int[numTris * 3];
int vertIndex = 0;
int triIndex = 0;
for (int i = 0; i < points.Length; i++)
{
Vector2 forward = Vector2.zero;
if (i < points.Length - 1 || isClosed)
{
forward += points[(i + 1)%points.Length] - points[i];
}
if (i > 0 || isClosed)
{
forward += points[i] - points[(i - 1 + points.Length)%points.Length];
}
forward.Normalize();
Vector2 left = new Vector2(-forward.y, forward.x);
verts[vertIndex] = points[i] + left * roadWidth * .5f;
verts[vertIndex + 1] = points[i] - left * roadWidth * .5f;
float completionPercent = i / (float)(points.Length - 1);
float v = 1 - Mathf.Abs(2 * completionPercent - 1);
uvs[vertIndex] = new Vector2(0, v);
uvs[vertIndex + 1] = new Vector2(1, v);
if (i < points.Length - 1 || isClosed)
{
tris[triIndex] = vertIndex;
tris[triIndex + 1] = (vertIndex + 2) % verts.Length;
tris[triIndex + 2] = vertIndex + 1;
tris[triIndex + 3] = vertIndex + 1;
tris[triIndex + 4] = (vertIndex + 2) % verts.Length;
tris[triIndex + 5] = (vertIndex + 3) % verts.Length;
}
vertIndex += 2;
triIndex += 6;
}
Mesh mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
return mesh;
}
}
What error do you get? A NullReferenceException
?
If so, are you sure the gameObject this script is attached to has a $$anonymous$$eshRenderer
component with a material?
NullReferenceException: Object reference not set to an instance of an object RoadCreator.UpdateRoad () (at Assets/1/RoadCreator.cs:24) RoadEditor.OnSceneGUI () (at Assets/1/RoadEditor.cs:15) System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ono$$anonymous$$ethod.cs:222) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ono$$anonymous$$ethod.cs:232) System.Reflection.$$anonymous$$ethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ethodBase.cs:115) UnityEditor.SceneView.CallOnSceneGUI () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/SceneView/SceneView.cs:2605) UnityEditor.SceneView.HandleSelectionAndOnSceneGUI () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/SceneView/SceneView.cs:1910) UnityEditor.SceneView.OnGUI () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/SceneView/SceneView.cs:1757) System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ono$$anonymous$$ethod.cs:222) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ono$$anonymous$$ethod.cs:232) System.Reflection.$$anonymous$$ethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/$$anonymous$$ethodBase.cs:115) UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/HostView.cs:295) UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/HostView.cs:288)
Please, provide the RoadEditor
too (edit your question to add the script)
Is UpdateRoad
called at runtime or at edit time?
Are you sure the gameObject this script is attached to has a $$anonymous$$eshRenderer component with a material?
But when I click on error it show 24th line:
GetComponent (). Shared$$anonymous$$aterial.mainTextureScale = new Vector2 (1, textureRepeat);
Read the error text, the error can "appear" at one line, but be caused for another thing.
If Hellium asks you something, do it, he always have a good answer.. :D
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Values not moving to Game Over screen or the Game over doesn't somehow work. 0 Answers
How to create a field of view for enemy ai that detects a player Unity 2d? 3 Answers
Instantiated object not showing in scene or hierarchy 2 Answers