c# – Unity – how Default Line material can be applied to line constructed using code, without using inpector to pass default material?


I have scene with camera, light source and empty object with an attached script.

I generate red line but its material is never set. Ho can I set, in script, its material as Default-Line? Without this it is shown as magenta, not red.

I know that it is root cause as setting material manually in editor fixes it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using System.Linq;
 
public class GraphDisplay : MonoBehaviour {
    void Start() {
        var a = new Vector3(0.0f, 0.0f, 0.0f);
        var b = new Vector3(0.0f, 0.0f, 10.0f);
        var pointArray = new Vector3[2];
        pointArray[0] = a;
        pointArray[1] = b;
        Color color = Color.red;
 
        var go = new GameObject("line");
        var lr = go.AddComponent<LineRenderer>();
            lr.startColor = color;
            lr.endColor = color;
        }
        lr.startWidth = 5;
        lr.endWidth = 5;
        lr.positionCount = pointArray.Length;
 
        lr.SetPositions(pointArray);
    }
}

I tried using recommended solution from https://gamedev.stackexchange.com/a/96966/96768 but that simply crashes with Trying to create a material from string - this is no longer supported.

Note that I want to set it entirely within code, without requiring any manual steps like setting some global variables using inspector.



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email