Question by
fenix73rus · Feb 26, 2021 at 10:51 PM ·
c#scripting problemerrorerror messagecreate
Сonstructor return null
Hello everyone. I can't understand why the constructor returns null? What is the problem? Both scripts are inherited from MonoBehaviour
public void Create()
{
float length = 2f;
int polygonNumber = 6;
if (_polygonal != null)
_polygonal.Destroy();
if (length < 1)
length = 1;
if (polygonNumber < 4)
polygonNumber = 4;
else if (polygonNumber > 8)
polygonNumber = 8;
var pointsPositions = CreatePolygonlPoints(length, polygonNumber);
var _material = lineRendererMaterials[Random.Range(0, lineRendererMaterials.Length)];
_polygonal = Instantiate(new Polygonal(pointsPositions, _material, length));
Debug.Log(_polygonal);
}
Script Polygonal
public Polygonal(List<Vector3> points, Material _material, float _length)
{
Length = _length;
PointCount = points.Count;
_gameObject = Instantiate(new GameObject(), Vector3.zero, Quaternion.identity);
var lineRenderer = _gameObject.AddComponent<LineRenderer>();
lineRenderer.material = _material;
lineRenderer.startWidth = 0.06f;
lineRenderer.endWidth = 0.06f;
lineRenderer.positionCount = points.Count - 1;
for(int i = 0; i < points.Count - 1; i++)
{
lineRenderer.SetPosition(i, points[i]);
}
}
I get error 2
NullReferenceException
UnityEngine.Object.Instantiate[T] (T original) (at <d1422b3fc93746018c92eda852993b93>:0)
PolygonalCreater.Create () (at Assets/Scripts/PolygonalCreater.cs:29)
UnityEngine.Events.InvokableCall.Invoke () (at <d1422b3fc93746018c92eda852993b93>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <d1422b3fc93746018c92eda852993b93>:0)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.4.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
Comment