Problem with unity editor namesapce
Hello, I have a script that randomize the trail renderer colors each time I instantiate a new one. The problem is that the only way I can change the colors is via SerializedObject.
public void RandomizeColor(TrailRenderer trailr) {
SerializedObject so = new SerializedObject(trailr);
so.FindProperty("m_Colors.m_Color[0]").colorValue = new Color(Random.value, Random.value, Random.value, 1);
so.FindProperty("m_Colors.m_Color[1]").colorValue = new Color(Random.value, Random.value, Random.value, 1);
so.FindProperty("m_Colors.m_Color[2]").colorValue = new Color(Random.value, Random.value, Random.value, 1);
so.FindProperty("m_Colors.m_Color[3]").colorValue = new Color(Random.value, Random.value, Random.value, 1);
so.FindProperty("m_Colors.m_Color[4]").colorValue = new Color(Random.value, Random.value, Random.value, 1);
so.ApplyModifiedProperties();
}
Then I use this method later on in the script. So it wprks but I can't build it as I'm using UnityEditor.
So my question is how do I work around that to be able to build the project.
Answer by Jessespike · Nov 24, 2015 at 08:06 PM
Create a new separate script and move the Editor code to it.
Or if you want, you can wrap the Editor related stuff around preprocessors, this can get messy real quick though:
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
Ok, so if I use it as an editor script in a Editor file, how do I use it through this script after ? Because using preprocessors only makes it build but the intersting part doesn't run so there is not any point for mer.
Oops, I missed the part where you wanted to do this after an instantiate. Hmm, I'm not even sure if it's possible.
Your answer
![](https://koobas.hobune.stream/wayback/20220612050055im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Help][Noob] My Enums/Input won't work 1 Answer
How to Undo a lot of created objects at once? [Solved] 1 Answer
Access an Array of class inside another Array of class? (EditorGUILayout) 1 Answer
I want to use a variable in the Unity editor source file to get it from a regular script. 0 Answers
(Help) I am having trouble with creating a simple 2D game in Unity. Please help. :) 0 Answers