I tried to get this function to show up in the event system for a GUI button so that I could manually end the turn by clicking a button:
public void TurnChange () {
if (TurnStatus == 0) {
TurnStatus = 1;
}
else if (TurnStatus == 1){
TurnStatus = 0;
}
}
From what I understand, it has to be public, void, and take one or none of a small list of parameters. All those things seem to be true, so why isn’t it showing up in the inspector as a function I can use? here’s the rest of the TurnManager script as it exists right now.
using UnityEngine;
using System.Collections;
public class TurnManager : MonoBehaviour {
public int TurnStatus =0;
// Use this for initialization
void Start () {
}
void Update () {
//Lets see if we can make some code to handle turn order
if (TurnStatus == 0) {
playerTurn ();
}
else if (TurnStatus == 1){
enemyTurn();
}
}
void playerTurn(){
Debug.Log ("Player's turn");
TurnStatus = 1; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
}
void enemyTurn(){
Debug.Log ("Enemy's turn");
TurnStatus = 0; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
}
public void TurnChange () {
if (TurnStatus == 0) {
TurnStatus = 1;
}
else if (TurnStatus == 1){
TurnStatus = 0;
}
}
}
So let me get that straight: You added the object which contains the Turn Manager to the onclick Event, and there is an entry in the dropdown menu what to do with it, for TurnManager, but inside there, there is no TurnChange? Did you save your file?
I tried it with exactly your code, and I can find the Method.
A few remarks to your code, I guess it is good to start using code conventions early:
Variables start with a lower case letter (c#)
Methods start with an upper case letter (c#)
If you have only those two possibilities, that either the player or the enemy can have a turn, an int is a bit of an overkill, a bool would be totally sufficient, and results more beautiful code.
Think your methods through. If you look at your update, and then the methods it is calling every frame, you would notice, that your TurnStatus changes every frame! (I don’t think you really want that).
Here Is a modified version of your code:
using UnityEngine;
using System.Collections;
public class TurnManager : MonoBehaviour {
public bool turnPlayer = true;
// Use this for initialization
void Start () {
}
void Update () {
//Lets see if we can make some code to handle turn order
if (turnPlayer) {
playerTurn ();
}
else {
enemyTurn();
}
}
void PlayerTurn(){
Debug.Log ("Player's turn");
//turnPlayer = false; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
}
void EnemyTurn(){
Debug.Log ("Enemy's turn");
//turnPlayer = true; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
}
public void TurnChange () {
turnPlayer = !turnPlayer;
}
}
Does exactly the same as your code, but is easier to read (e.g. you know which status is the playerTurn, and which on is the enemy turn) and cleaner.
It’s probably obvious at this point, but I am very new to C#. I’m actually proud of how far I’ve come in a few weeks. You are correct. As is seen in the picture below, I have the EndTurnButton as an object and I’ve dragged the TurnManager script onto it, but no function shows up. I get the feeling that I just don’t understand something about how Unity works and that my code is fine. I’ve double checked to ensure the code is saved and up to date. The Unity scene and project have been saved. Am I missing something obvious?
I had a plan to, once I had figured out what was wrong, set it up so that I could have AI allies as well, which would require three turn statuses. I had planned, originally, to put all the things that occur during your turn inside the PlayerTurn and EnemyTurn before it gets to the part in which it changes the turn but, instead, I think I’ll make the code for the things that you do during your turn, then just call on the TurnChange function inside that code. I just sorta made all this up in my head. I’m not working off of any tutorials or example scripts.
It appears as though I didn’t have an object that I tossed the script onto before putting it into the onClick event. After testing that, it seems to be working. Thank you so much for all your help. I’m sure I’ll get in over my head again soon. Keep an eye out for my silly questions in the future.
After fiddling with it, I quoted out the part where it changes the turn status in the update method so that I could test the button, but it’s still changing status over and over again. I thought that with that portion quoted out, it wouldn’t change the turn status unless I hit that button.
using UnityEngine;
using System.Collections;
public class TurnManager : MonoBehaviour {
public int turnStatus =0;
// Use this for initialization
void Start () {
}
void CurrentTurn () {
//Lets see if we can make some code to handle turn order
if (turnStatus == 0) {
playerTurn ();
}
else if (turnStatus == 1){
enemyTurn();
}
}
void playerTurn(){
Debug.Log ("Player's turn");
//TurnStatus = 1; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
}
void enemyTurn(){
Debug.Log ("Enemy's turn");
//TurnStatus = 0; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
}
public void TurnChange () {
if (turnStatus == 0) {
turnStatus = 1;
}
else if (turnStatus == 1){
turnStatus = 0;
}
}
}
Uhm, with the code you posted, you actually shouldn’t get any Debug output at all! Do you get output in the console?
If you do
→ Did you save?
→ Maybe there is an other script in your scene, causing this output. Wait till you get output, and then stop the game. Afterwards you can double click on a console entry, and see from where it is coming.
If you don’t
→ Does any other function call TurnChange? Add a Debug.Log(“TurnChange got Called”); at line 34 to see what happens.
I’ve loved having your help and I’m struggling my way through some difficult stuff. This problem, however, has been fully resolved. If you’d like to follow my progress, I’ve opened a new thread about struggling against my disfunctional grid system over here. http://forum.unity3d.com/threads/creating-interactive-grid-for-tactics-game.290728/ If not, thanks a TON for all your help!