Hi everyone,
I could’nt find a specific solution on the forum for my issue. This is my code
if(gameController.updateTheTime)
{
AddTime ();
}
public void AddTime()
{
UpdateTime ();
}
void UpdateTime()
{
countDown += 5;
}
My scene setup is as follows:
- I have a bool ‘updateTheTime’ set to FALSE on my Game Controller, whenever i collect a ‘Shuriken’ the bool is set to TRUE. The bool ‘updateTheTime’ feeds into my ‘PlayStateScene’ (code above) and increments my countDown by +5 seconds.
I currently have 3 ‘Shurikens’ in the scene but i only get +5 seconds when i collect the first Shuriken the other 2 for some reason doesn’t increment my time. I tried using a ‘for’ loop to keep track of all of the ‘Shurikens’ in my scene however im unsure of how to approach the syntax as i haven’t used a Loop yet 
All i want to do is have a reference to all of the ‘Shurikens’ in my scene and increment the countDown whenever i collect a ‘Shuriken’ Assistance will be greatly appreciated…Thanx!!!
for(int i = 0; i<totalNumberOfShurikens;i++)
{
if(gameController.updateTheTime)
{
AddTime ();
}
}
what is the point of the AddTime() function? it just calls another function without adding anything…
how are you doing this? if the issue is that the subsequent collections aren’t working it would help to know how you are handling “collecting stuff”…
can’t help but think you’re looking at this a little upsidedown… if you’re using triggers/colliders to collect the items you could just have a “AddTime(float)” function on you’re player that is called when the player runs into the shuriken using sendMessage so it can provide the float parameter for the amount of time to add:
Thanx for the reply,
I’ve been following various tutorials and books on programming up to this point im more of a artist :). I’m quite far into development and i have yet to write any code which requires collecting things/data. The AddTime() i used as i copied it from a AddScore() method which i got from reading a book, my Game Controller code below:
using UnityEngine;
using System.Collections.Generic;
public class GameController : MonoBehaviour
{
//public Texture2D beginStateSplash;
//public Texture2D lostStateSplash;
//public Texture2D wonStateSplash;
//public Texture2D levelSelectStateSplash;
public GameObject gameManager;
public bool updateTheTime = false;
[HideInInspector]
public int score = 0;
public int sceneBeginningScore = 0;
public List<GameObject> cameras;
Shuriken shuriken;
private StateManager manager;
void Start()
{
score = 0;
GameObject gameManagerObj;
gameManagerObj = GameObject.Find ("GameManager");
if (gameManagerObj != null)
{
manager = gameManagerObj.GetComponent<StateManager> ();
Debug.Log ("StateManager Component is available in GameController");
} else
Debug.Log ("StateManager Component NOT available in GameController");
}
public void AddScore (int newScoreValue)
{
UpdateScore ();
}
void UpdateScore()
{
score += 10;
}
public void Restart()
{
//Restart function not being used
Destroy (gameObject);
Application.LoadLevel ("2D Game Scene");
}
void Update()
{
if (score >= 140)
//All Shurikens collected should unlock a gold star ranking
//Should not end level
//Application.LoadLevel("2D_Game_Scene1");
Debug.Log ("Congratulations! You have collected all of the Shurikens");
return;
}
public void ResetPlayer()
{
score = sceneBeginningScore;
}
public void SetScore()
{
sceneBeginningScore = score;
}
}
This is the code on my Shuriken GameObject which increases the score when collected
using UnityEngine;
using System.Collections;
public class Shuriken : MonoBehaviour
{
private GameController gameController;
[HideInInspector]
public int newScoreValue;
void Start()
{
GameObject gameControllerObject;
gameControllerObject = GameObject.Find ("GameManager");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController> ();
}
if (gameController == null)
{
Debug.Log ("Cant find any 'GameController' scripts! ");
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("Player is available in 'Shuriken' if statement");
if(gameController != null)
{
gameController.AddScore(newScoreValue);
Destroy (gameObject);
Debug.Log ("Player collected a shuriken!!!");
}
else
Debug.Log ("GameController is not available in 'Shuriken' if statement");
}
}
}
I’m using the exact same code as above with the minor adjustments for the ‘Shurikens’ which increases the time when collected. I believe the reason why my time is only increasing only once when i collect the first ‘Shuriken’, might be due to the fact that the Timer code is in my ‘PlayState’ code and not on the ‘GameController’ code.
Well, just from looking at it, I don’t see where you are changing “update the time” from false to true… so technically it would never call the time updater.
Thank you for the reply,
The ‘Shuriken’ script above was to answer the question of @LeftyRighty in regards to how i came across the AddScore()/AddTime() methods. I have a different script for the ‘Shuriken_AddTime’.
using UnityEngine;
using System.Collections;
public class Shuriken_AddTime : MonoBehaviour
{
private GameController gameController;
[HideInInspector]
public int newScoreValue;
void Start()
{
GameObject gameControllerObject;
gameControllerObject = GameObject.Find ("GameManager");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController> ();
}
if (gameController == null)
{
Debug.Log ("Cant find any 'GameController' scripts! ");
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("Player is available in 'Shuriken_AddTime' if statement");
if(gameController != null)
{
gameController.updateTheTime = true;
gameController.AddScore(newScoreValue);
Destroy (gameObject);
Debug.Log ("Player collected a Shuriken_AddTime!!!");
}
else
Debug.Log ("GameController is not available in 'Shuriken' if statement");
}
}
}
This i my attempt in using a foreach loop in my Game Controller to inform my ‘StateManager’ (PlayStateScene) to add 5 seconds every time a ‘ShurikenAddTime’ is collected. It works when i collect the first one, but not the remaining gameobjects in the scene.
Game Controller in Update():
shurikenAddTimeObjs = GameObject.FindGameObjectsWithTag ("Shuriken_AddTime");
foreach(GameObject sObj in shurikenAddTimeObjs)
{
Shuriken_AddTime SDT = sObj.GetComponent<Shuriken_AddTime>();
if(SDT != null)
{
if(updateTheTime) // True value received from 'Shuriken_AddTime' gameObject
{
Debug.Log("UpdateTheTimeNow will be set to true and pushed to PlayStateScene_1");
updateTheTimeNow = true; //True value used to send to PlayStateScene to add 5 seconds when collected
}
}
}
PlayStateScene:
//Reference to gameController
GameObject gameControllerObject;
gameControllerObject = GameObject.Find ("GameManager");
if (gameControllerObject != null)
{
Debug.Log ("Game Controller is available in PlayStateScene_1");
gameController = gameControllerObject.GetComponent<GameController> ();
}
if (gameController == null)
{
Debug.Log ("Game Controller is NOT available in PlayStateScene_1");
}
//When to add time to countDown
//Everytime a 'Shuriken_AddTime' is collected
if(gameController != null)
{
if(gameController.updateTheTimeNow)
{
Debug.Log ("5 seconds will be added to the Timer");
countDown += 5;
}
}
Please let me know what I’m doing wrong. Thank you in advance!
For anyone following this thread, i was able to collect two ‘Shuriken_AddTime’ gameobejcts and increase the countDown timer twice only. I created a method which reset the two booleans in my Game Manager to false once it has been collected
shurikenAddTimeObjs = GameObject.FindGameObjectsWithTag ("Shuriken_AddTime");
foreach(GameObject sObj in shurikenAddTimeObjs)
{
Shuriken_AddTime SDT = sObj.GetComponent<Shuriken_AddTime>();
if(SDT != null)
{
if(updateTheTime)
{
Debug.Log("UpdateTheTimeNow will be set to true and pushed to PlayStateScene_1");
updateTheTimeNow = true;
Invoke("ResetBooleanValues", 1f); // This helped a little only as i can only collect two and not three :(
}
}
}
When i reset my boolean values, to allow me to collect another ‘Shuriken_AddTime’ gameobject the actual time gets reset as well 
Does anyone have any suggestions on how i can work around this issue???
Hi again,
I found that moving all of the Timer Count Down and AddTime code to my Game Manager instead of keeping it in my PlayState is more effective and easier to manage.