- Home /
How to make the loop work
using UnityEngine; using System.Collections;
public class SilverFernScript : MonoBehaviour { // Use this for initialization void Start() {
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < 10; i++)
{
// Moves the silver fern forward along its z axis 1 unit/second.
transform.Translate(Vector3.forward * Time.deltaTime);
// Moves the silver fern upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("User"))
{
Renderer ren = GetComponent<Renderer>();
if (ren.material.GetColor("_Color") == Color.cyan)
{
yellow();
}
else if (ren.material.GetColor("_Color") == Color.yellow)
{
red();
}
else if (ren.material.GetColor("_Color") == Color.red)
{
Cyan();
}
}
}
void Cyan()
{
Renderer rend = GetComponent<Renderer>();
//changes the color to cyan
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.cyan);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.cyan);
}
void yellow()
{
Renderer rend = GetComponent<Renderer>();
//changes the color to yellow
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.yellow);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.yellow);
}
void red()
{
Renderer rend = GetComponent<Renderer>();
//changes the color to red
rend.material.shader = Shader.Find("Color");
rend.material.SetColor("_Color", Color.red);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red);
}
}My loop doesn't seem to work. Also, is GetComponent a function?. Please Help!!
Answer by Zodiarc · Sep 26, 2018 at 09:01 AM
It works but you need to understand that Update() is called every frame and the frame won't "end" until all Update() calls are ended. That's why Unity locks itself if you have an endless loop somewhere.
The loop runs through in this one frame, so you'd probably see the GameObject just jump to its end position. You'll need to rewrite it like that to make it work:
public class SilverFern: MonoBehaviour {
private int index = 0;
public void Update() {
if(index < 10) {
// move your fern here
index++;
}
}
}
And yes, GetComponent is a function of GameObject. The "special" thing about it is, that it's a generic function which takes the data type as an argument. If you want to know more, read here.
After looking more closely on your code. What are you trying to achieve with the loop? There is no need for a loop or anything. Just call the Translate method and it will work. Please read the Documentation and watch some tutorials because it is obvious that you don't know the basics.
Answer by UnityCoach · Sep 26, 2018 at 09:02 AM
We have already answered this in this other question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OverlapSphere for parallel arrays 1 Answer
Checking an array variable in C# vs JavaScript 3 Answers
C# List looping Vector3's 2 Answers