- Home /
Question by
TheOneMiner · Mar 26 at 04:34 PM ·
scripting problemscript.timertime.time
Timestamp cooldown doesn't work!
Greetings!
So I am trying to make this usable item (a book) and I want it to take time to do this "assignment" (about 15 seconds). I have tried to use time stamps but that just doesn't do anything. It seems that Time.time just doesn't get to the time stamp. Is there any other way of doing this or is it just my code issue?
Here's the code do assignment function:
public void DoAssignments()
{
if (GetComponent<EquipmentManager>().currentItem.assignmentsToDo > 0)
{
assignmentStamp = Time.time + assignmentTime;
if (assignmentStamp <= Time.time)
{
print("Assignment done");
GetComponent<EquipmentManager>().currentItem.assignmentsToDo -= 1;
}
}
}
Comment
Answer by rh_galaxy · Mar 27 at 01:55 PM
You are setting the assignmentStamp every time, so the condition is never fulfilled. Also we don't know when/how often you run DoAssignment(). Here is my suggestion:
EquipmentManager em;
public void Start()
{
em = GetComponent<EquipmentManager>(); //save this for future use
}
bool bAssignmentSet = false;
void Update() //this runs every frame
{
if (!bAssignmentSet && em.currentItem.assignmentsToDo > 0)
{
assignmentStamp = Time.time + assignmentTime;
bAssignmentSet = true;
}
if (bAssignmentSet && assignmentStamp <= Time.time)
{
em.currentItem.assignmentsToDo -= 1;
bAssignmentSet = false; //ready for new assignment
print("Assignment done");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613054640im_/https://answers.unity.com/themes/thub/images/avi.jpg)