- Home /
Having some problems with randomization
Hey guys, I am trying to add some more randomization in to this procedurally generated fractal script and it is pitching some fits that i cannot seem to fix.
here is the script using UnityEngine; using System.Collections;
public class Fractal : MonoBehaviour
{
public Mesh mesh;
public Material material;
private int depth;
public int maxDepth;
public float childScale;
private Material[] materials;
private float _nr = Random.Range(-80f, -100f);
private float _r = Random.Range(80f, 100f);
private Color RandomColour()
{
float r = Random.value;
float g = Random.value;
float b = Random.value;
return new Color(r, g, b);
}
private void InitializeMaterials ()
{
materials = new Material[maxDepth + 1];
for (int i = 0; i <= maxDepth; i++)
{
materials[i] = new Material(material);
material.color = RandomColour();
}
}
private void Start ()
{
if (materials == null)
{
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>().material = materials[depth];
gameObject.AddComponent<Material>
if (depth < maxDepth)
{
StartCoroutine(CreateChildren());
}
}
private static Vector3[] childDirections =
{
Vector3.up,
Vector3.right,
Vector3.left,
Vector3.forward,
Vector3.back;
}
private static Quaternion[] childOrientations =
{
Quaternion.identity,
Quaternion.Euler(0f, 0f, _nr),
Quaternion.Euler(0f, 0f, _r),
Quaternion.Euler(_r, 0f, 0f),
Quaternion.Euler(_nr, 0f, 0f);
}
private IEnumerator CreateChildren ()
{
for (int i = 0; i < childDirections.Length; i++)
{
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
new GameObject("Fractal Child").AddComponent<Fractal>().Initialize(this, i);
}
}
private void Initialize (Fractal parent, int childIndex)
{
mesh = parent.mesh;
material = parent.material;
maxDepth = parent.maxDepth;
depth = parent.depth + 1;
childScale = parent.childScale;
transform.parent = parent.transform;
transform.localScale = Vector3.one * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
}
}
and the errors that i am getting are Assets/Fractal Stuff/Fractal.cs(51,18): error CS1525: Unexpected symbol if' Assets/Fractal Stuff/Fractal.cs(62,29): error CS1525: Unexpected symbol
;', expecting ,', or
}'
Assets/Fractal Stuff/Fractal.cs(65,15): error CS8025: Parsing error
i have a feeling it is something stupid i missed but i cannot seem to find it so any help would be appreciated
You have a simple syntax problem:
You need to put the semi-colon after your array declaration.
ok so now my errors are
Assets/Fractal Stuff/Fractal.cs(51,18): error CS1525: Unexpected symbol `if'
Assets/Fractal Stuff/Fractal.cs(56,34): error CS0116: A namespace can only contain types and namespace declarations
Assets/Fractal Stuff/Fractal.cs(71,46): error CS1525: Unexpected symbol `;', expecting `,', or `}'
and
Assets/Fractal Stuff/Fractal.cs(74,15): error CS8025: Parsing error
Just checking... following Vesuvian's advice, your code should look as follows:
private static Vector3[] childDirections =
{
Vector3.up,
Vector3.right,
Vector3.left,
Vector3.forward,
Vector3.back
};
private static Quaternion[] childOrientations =
{
Quaternion.identity,
Quaternion.Euler(0f, 0f, _nr),
Quaternion.Euler(0f, 0f, _r),
Quaternion.Euler(_r, 0f, 0f),
Quaternion.Euler(_nr, 0f, 0f)
};
it does now, but all that did was add more errors...
the errors are now
Assets/Fractal Stuff/Fractal.cs(51,18): error CS1525: Unexpected symbol `if'
Assets/Fractal Stuff/Fractal.cs(56,34): error CS0116: A namespace can only contain types and namespace declarations
Assets/Fractal Stuff/Fractal.cs(65,37): error CS0116: A namespace can only contain types and namespace declarations
Assets/Fractal Stuff/Fractal.cs(74,29): error CS0116: A namespace can only contain types and namespace declarations
Assets/Fractal Stuff/Fractal.cs(83,22): error CS0116: A namespace can only contain types and namespace declarations
Assets/Fractal Stuff/Fractal.cs(95,1): error CS8025: Parsing error
Answer by SnotE101 · Dec 12, 2014 at 01:59 AM
Line 47 gameObject.AddComponent
should be gameObject.AddComponent();
Answer by junkdog8 · Dec 12, 2014 at 07:42 PM
figured it out here is the finished script if anyone wants to try it out...
using UnityEngine;
using System.Collections;
public class Fractal : MonoBehaviour
{
public Mesh[] meshes;
public Material material;
public int maxDepth;
private int depth;
public float childScale;
public float maxRotationSpeed;
private float rotationSpeed;
public float spawnProbability;
private Material[,] materials;
public float maxTwist;
private void InitializeMaterials ()
{
materials = new Material[maxDepth + 1, 2];
for (int i = 0; i <= maxDepth; i++)
{
float t = i / (maxDepth - 1f);
t *= t;
materials[i, 0] = new Material(material);
materials[i, 0].color = Color.Lerp(Color.white, Color.yellow, t);
materials[i, 1] = new Material(material);
materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
}
materials[maxDepth, 0].color = Color.magenta;
materials[maxDepth, 1].color = Color.red;
}
private void Start ()
{
rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
transform.Rotate(Random.Range(-maxTwist, maxTwist), 0f, 0f);
if (materials == null) {
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(0, meshes.Length)];
gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
if (depth < maxDepth) {
StartCoroutine(CreateChildren());
}
}
private static Vector3[] childDirections =
{
Vector3.up,
Vector3.right,
Vector3.left,
Vector3.forward,
Vector3.back
};
private static Quaternion[] childOrientations =
{
Quaternion.identity,
Quaternion.Euler(0f, 0f, -90f),
Quaternion.Euler(0f, 0f, 90f),
Quaternion.Euler(90f, 0f, 0f),
Quaternion.Euler(-90f, 0f, 0f)
};
private IEnumerator CreateChildren ()
{
for (int i = 0; i < childDirections.Length; i++)
{
if (Random.value < spawnProbability)
{
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
new GameObject("Fractal Child").AddComponent<Fractal>().Initialize(this, i);
}
}
}
private void Initialize (Fractal parent, int childIndex)
{
meshes = parent.meshes;
materials = parent.materials;
maxDepth = parent.maxDepth;
depth = parent.depth + 1;
childScale = parent.childScale;
spawnProbability = parent.spawnProbability;
maxRotationSpeed = parent.maxRotationSpeed;
maxTwist = parent.maxTwist;
transform.parent = parent.transform;
transform.localScale = Vector3.one * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
}
private void Update ()
{
transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613173821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I debug a Linux crash? 1 Answer
Generic Debug Message 1 Answer
How do I keep Debug.Log in archive/release iOS builds? 0 Answers
Android Debug USB 7 Answers
Debug.Log() remove callstack ? 1 Answer