Why does Debug.Log continue to execute from Start() and why does InvokeRepeating() not?
While trying to figure out why InvokeRepeating() doesn't execute I noticed the line: Debug.Log("Initial moveSpeed: " + moveSpeed); continuously executes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformControl : MonoBehaviour
{
[SerializeField] float moveSpeed;
private Vector3 randomRotation;
private Rigidbody platformRigidbody;
private float removePositionZ;
// Start is called before the first frame update
void Start()
{
moveSpeed = 1500f;
platformRigidbody = GetComponent<Rigidbody>();
randomRotation = new Vector3(Random.Range(0f, 15f), Random.Range(-25f, 25f), 0f);
transform.Rotate(randomRotation);
removePositionZ = Camera.main.transform.position.z;
Debug.Log("Initial moveSpeed: " + moveSpeed); // This line is the issue... why?
// After ten seconds begin to increase moveSpeed every second
//InvokeRepeating("MoveSpeedControl", 10.0f, 1.0f);
}
// Update is called once per frame
void Update()
{
MovementControl();
}
void MovementControl() // Controls platform movement
{
Vector3 movementVector = new Vector3(0f, 0f, moveSpeed * Time.deltaTime);
platformRigidbody.velocity = movementVector;
if(transform.position.z > removePositionZ){ Destroy(gameObject); }
}
void MoveSpeedControl()
{
Debug.Log("Enter MoveSpeedControl");
while(moveSpeed < 2000)
{
moveSpeed += 1;
Debug.Log("Current Move Speed: " + moveSpeed);
}
}
}
At first I thought the InvokeRepeating() was somehow interfering (hence why it is commented out) but that isn't the case. I'm hoping,I have been having trouble getting the InvokeRepeating to execute; I use it in another script without issue but this one doesn't seem to cooperate.
I'm hoping someone else can see what I cannot. Thanks for the assistance!
Answer by Nate1576 · Jan 03 at 03:18 AM
I determined the reasoning for this; if my understanding of prefabs is correct.
Each instance of the platform prefab brought a new instance of the script. So, because of that, the Debug.Log() message was being displayed with each new instance of the prefab.
Were the instances to stay for the 10 seconds, that part of the code would have been executed as well.
Your answer
Follow this Question
Related Questions
How can I improve this code? 1 Answer
Im doing a Countdown on Debug. and it works but the numbers repeat. 1 Answer
[Absolute beginner] Play animation on trigger (trigger = several fallen objects) script 1 Answer
HingeJoint motor will not change values? 1 Answer
Jump animation not returning to normal? 0 Answers