using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
private float speed;
public bool startnow = false;
// Use this for initialization
void Start () {
speed = 0;
Debug.Log(startnow);
}
public void atstart()
{
speed = 0.8f;
}
// Update is called once per frame
void Update () {
Debug.Log("Value of speed: "+speed);
}
}
I am calling atstart at runtime from other class after 2sec here is the code
What i am doing is trying to change the value of speed at run time, at starting iam getting output “value of speed: 0” but after 2 sec atstart() function calls but still value of speed is showing 0, what is wrong why the value of speed is not changing.
The code doesn’t seem to be incorrect, I take it you haven’t gotten any errors, otherwise you probably would’ve told us.
I noticed you use:
spike[r].GetComponent<Test>().atstart();
spike[r] indicating that you have an array of Spikes, and you use index r and call that specific spike and change its value speed to 0.8f
I believe it’s working, but if you have multiple Spikes, you probably have multiple “Test” scripts, which leads to multiple updates doing:
Debug.Log("Value of speed: "+speed);
So, you’d end up with 999+ Logs, most of which still having speed at 0.
Try getting rid of the Debug.Log in Update and put the Debug.Log in the “atstart” method, then you’ll know for sure if it’s called at all and whether the value is applied or not.
I hope that helps :3
i am instantiating only one element in my array still getting the same result, i have given all other details on @khenkel comments, i dont understand why its not working :(
Hey @ThePersister i have solved my problem, issue is i am just instantiating my gameobject and forgot to assign it to spike variable, finally i solved my issue and thanks for your support :)
I can’t spot any errors in your code right now so I suppose that your obstacleGenerator Start() doesn’t get called at all. Maybe there’s no instance of your object, or you’re accessing the wrong object in your obstacleGenerator() method. In this case a good way to debug is to insert Debug.Log() in each method and just check if something is missing.
On a side note, I recommend to not invoke specific methods after a “magic” delay to ensure that everything else has been called before. Instead, try to invoke your methods strictly in a row, so you can be 100% sure that everything gets called in your desired order.
i have taken your recommendation and i have change the method as public void atstart() { speed = 0.8f; Debug.Log("Method called "+speed) } i am getting the output as "Method called 0.8" but still update function will printing value of speed as 0 i dont understand where the code will resetting its value. And ya i have used invoke for same reason, so that i will make sure that value of speed initialize as 0 before method resets its value
i am instantiating only one element in my array still getting the same result, i have given all other details on @khenkel comments, i dont understand why its not working :(
– stblueHey @ThePersister i have solved my problem, issue is i am just instantiating my gameobject and forgot to assign it to spike variable, finally i solved my issue and thanks for your support :)
– stblueI'm glad I could help, good job on solving your issue! ;D
– ThePersister