Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need help making temporary pick-ups (on-click) within game.

Discussion in 'Scripting' started by theoriginalmedz, Nov 21, 2018.

  1. theoriginalmedz

    theoriginalmedz

    Joined:
    Dec 1, 2016
    Posts:
    3
    Hi guys, need help making a temporary pick-up within the game on mouse click on the UI

    e.g. a pick-up called the Boots of Hermes lasts for 5 seconds after being picked up. The pick-up is placed in the game world and once picked up, the UI button for it changes and when clicked, it changes the players speed for 5 seconds then turns off. I've got the script for picking it up and awarding the player some score and then the UI button for it changes from greyed out to colored to indicate to the player it is picked up however the script for on click in the inspector I can't seem to get it working. It seems like it would change the speed but not change back after 5 seconds (using coroutines). I can post the codes i've been trying to use but can't for the life of me figure it out as I am not an expert haha. also, how do I do it when the pick-up is picked up,the player can only use it once.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class onClickHermes : MonoBehaviour
    7. {
    8.  
    9.     public playerScript pScript;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.     }
    16.  
    17.  
    18.  
    19.  
    20.     public void changeSpeed()
    21.     {
    22.         StartCoroutine(myTime()); //this is what is clicked on mouse click
    23.      
    24.      
    25.     }
    26.  
    27.     IEnumerator myTime()
    28.     {
    29.         pScript.movementSpeed -= -15f;
    30.         print("speedchange");
    31.         yield return new WaitForSeconds(5f);
    32.         pScript.movementSpeed += 10f;
    33.         print("speeddefault");
    34.      
    35.      
    36.    }
    37. }
     
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    At the beginning of the coroutine, you add speed by subtracting a negative value, and at the end you add speed again.
     
  3. theoriginalmedz

    theoriginalmedz

    Joined:
    Dec 1, 2016
    Posts:
    3
    hi Reeii thank you for the quick reply, what do you mean by this? You mean within my code itself or something that I need to do?

    EDIT: I see you mean within the code, I saw it while researching but it works - just doesn't change after 5 seconds. Do you know how to fix?
     
  4. theoriginalmedz

    theoriginalmedz

    Joined:
    Dec 1, 2016
    Posts:
    3
    nevermind now, Reeii you were right I fixed it. The values were in the wrong place thank you