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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Vars through scripts.

Discussion in 'Scripting' started by chemimartinez77, Jun 10, 2019.

  1. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    Hi all,

    I need to change the shape of a country once reached some phase of the game. The thing is that I have a common script that already changes that shape. I'd like to manipulate that script dynamically and I don't know how.

    I post a video and the 3 scripts I considered most important.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ZoomObject : MonoBehaviour {
    6.  
    7.     private Color overColor = GameConstants.ConvertColor(0, 255, 255, 255);
    8.     private Color selectColor = GameConstants.ConvertColor(150, 0, 0, 255);
    9.     private Color originalColor;
    10.     private Vector3 originalScale;
    11.     private Vector3 originalPosition;
    12.  
    13.     private bool mustZoom;
    14.     private bool highlighted = false;
    15.     private bool selected = false;
    16.     float x = 0f;
    17.     float y = 0f;
    18.     private void Start(){
    19.         mustZoom = false;
    20.         originalScale = transform.localScale;
    21.         originalPosition = transform.position;
    22.         originalColor = GetComponent<SpriteRenderer>().color;
    23.     }
    24.  
    25.     private void Update(){
    26.         if (mustZoom){
    27.             mustZoom=false;
    28.             StartCoroutine("ZoomIn");
    29.         }
    30.     }
    31.  
    32.     private void OnMouseEnter() {
    33.        
    34.         highlighted = true;
    35.         GetComponent<SpriteRenderer>().color = overColor;
    36.         mustZoom=true;
    37.         transform.position=new Vector3(transform.position.x, transform.position.y, transform.position.z - 20);
    38.         x = transform.localScale.x*1.015f;
    39.         y = transform.localScale.y*1.015f;
    40.     }
    41.  
    42.  
    43.     IEnumerator ZoomIn(){
    44.         while (x < originalScale.x*1.3 && y < originalScale.y*1.3){
    45.             transform.localScale=new Vector3 (x, y, transform.localScale.z);
    46.             x = transform.localScale.x*1.015f;
    47.             y = transform.localScale.y*1.015f;
    48.             yield return new WaitForSeconds(.01f);      
    49.         }
    50.        
    51.     }
    52.    
    53.  
    54.     private void OnMouseExit() {
    55.         highlighted = false;
    56.         StopCoroutine("ZoomIn");
    57.         if (!selected){
    58.             GetComponent<SpriteRenderer>().color = Color.white;
    59.         } else {
    60.             GetComponent<SpriteRenderer>().color = selectColor;
    61.         }
    62.         SetOriginalTransform();
    63.     }
    64.  
    65.     private void SetOriginalTransform(){
    66.         transform.localScale=originalScale;
    67.         transform.position=originalPosition;
    68.     }
    69.  
    70. // Esto no va aquí. Zoom object debe encargarse sólo del zoom
    71.     private void CheckSelectCountry(){ // Esto no va aquí. Zoom object debe encargarse sólo del zoom
    72.         bool rightClick = false;
    73.         bool leftClick = false;
    74.         //bool middleClick = false;
    75.         if(Input.GetMouseButtonDown(0)) leftClick = true;
    76.         if(Input.GetMouseButtonDown(1)) rightClick = true;
    77.         //if(Input.GetMouseButtonDown(2)) middleClick = true;
    78.  
    79.         if (highlighted){
    80.             if (leftClick && !selected){
    81.                 GetComponent<SpriteRenderer>().color = selectColor;
    82.                 selected = true;
    83.                 SetOriginalTransform();
    84.             } else if (rightClick && selected){
    85.                 GetComponent<SpriteRenderer>().color = originalColor;
    86.                 selected = false;
    87.                 SetOriginalTransform();
    88.             }
    89.         }
    90.     }
    91.  
    92. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6.  
    7. public class GameController : MonoBehaviour {
    8.     //public CanvasController canvasController;
    9.     public Country[] countries;
    10.     public Player[] players;
    11.     public int playerIndex = 0;
    12.     public int maxCountries = 6;
    13.     public Button nextPlayerButton;
    14.     public Image currentPlayerImage;
    15.     public Text currentPlayerName;
    16.     int unitsDropped = 0;
    17.  
    18.     public Text phaseText;
    19.  
    20.     public Player currentPlayer = null;
    21.     //public int maxPlayers = 3;
    22.  
    23.     public GameConstants.Phase currentPhase;
    24.     private bool phaseChange = false;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start(){
    28.         currentPlayer = players[playerIndex];
    29.         currentPhase = GameConstants.Phase.Start;
    30.         currentPlayer.droppedArmy = false;
    31.         currentPlayerImage.sprite = currentPlayer.meeple;
    32.         currentPlayerName.text = currentPlayer.playerName;
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update(){ //EN REALIDAD ESTO NO SE TIENE QUE HACER EN CADA PUTO FRAME. UN EVENTO CLICK LO DISPARARÁ.
    37.         // if (phaseChange){
    38.         //     changePhase();
    39.         // }
    40.        
    41.     }
    42.  
    43.     public void clickOnCountry(Country countryClicked){
    44.         switch (currentPhase){
    45.             case GameConstants.Phase.Start:
    46.                 GameConstants.ClearConsole();
    47.                 Debug.Log("FASE DE INICIO. DROPEAR EJERCITOS POR TODO EL MAPA");
    48.                         Debug.Log("dsadsadasdas: "+countryClicked.gameController.currentPhase);
    49.  
    50.                 if (!currentPlayer.droppedArmy && currentPlayer.isTurn){ //¿Dropeo army?
    51.                     Debug.Log("- Hay que dorpear army. "+currentPlayer.playerName);
    52.                     if (maxCountries > unitsDropped){ //¿Hay algún país vacío?
    53.                         Debug.Log("- Hay países vacíos. maxCountries: "+ maxCountries);
    54.                         if (countryClicked.army.units == 0){ //Si se hace click sobre un país vacío
    55.                             countryClicked.army.units += 1;
    56.                             Debug.Log("- countryClicked.army.units: "+countryClicked.army.units);
    57.                             countryClicked.army.gameObject.SetActive(true);
    58.                             countryClicked.army.GetComponent<SpriteRenderer>().sprite = currentPlayer.meeple;
    59.                             countryClicked.owner = currentPlayer;
    60.                             currentPlayer.droppedArmy = true;
    61.                             unitsDropped+=1;
    62.                             changeTurn();
    63.                         }
    64.                     } else {
    65.                         Debug.Log("No hay paises vacíos");
    66.                         if (countryClicked.owner == currentPlayer && currentPlayer.currentUnits > 0){
    67.                             Debug.Log("- Hay que dropear en el propio país");
    68.                             currentPlayer.currentUnits -=1;
    69.                             countryClicked.army.units += 1;
    70.                             Debug.Log("countryClicked.army.units: "+countryClicked.army.units);
    71.                             countryClicked.army.textUnits.text = "x"+countryClicked.army.units.ToString();
    72.                             unitsDropped+=1;
    73.                             currentPlayer.droppedArmy = true;
    74.                             changeTurn();
    75.                         } else if (countryClicked.owner != currentPlayer ){
    76.                             Debug.Log("- Hay que dropear en OTRO país");
    77.                         } else {
    78.                             //FIN DE FASE
    79.                             Debug.Log("SACABO LA FASE INICIAL - Todos los países están ocupados");
    80.                             changePhase();
    81.                         }
    82.                     }
    83.                 } else { //Poner a parpadear el botoncico de next player
    84.                     //Debug.Log("//Hay que activar el botón de cambio de turno");
    85.                     //StartCoroutine(nextPlayerButton.GetComponent<InAndOut>().ZoomInOut());
    86.                     //De momento cambio automático de turno
    87.                     //changeTurn();
    88.                 }
    89.                 countryClicked = null;
    90.                 break;
    91.             case GameConstants.Phase.Combat:
    92.                 phaseText.text = "Combate";
    93.                     countryClicked.GetComponent<SpriteRenderer>().color = GameConstants.ConvertColor(255,0,255,1);
    94.                 Debug.Log("FASE DE COMBATE. SELECCIONAR DEFENSOR");
    95.                 break;
    96.             case GameConstants.Phase.Relocate:
    97.                 phaseText.text = "Ubicación";
    98.                 Debug.Log("FASE DE REUBICACIÓN. SELECCIONAR 2 CIUDADES CONECTADAS ENTRE SÍ");
    99.                 break;
    100.             case GameConstants.Phase.Reinforcement:
    101.                 phaseText.text = "Combate";
    102.                 Debug.Log("FASE DE REFUERZOS.");
    103.                 break;
    104.         }
    105.     }
    106.  
    107.     public void changeTurn(){
    108.         StopAllCoroutines();
    109.         //Debug.Log("last player: "+currentPlayer.name);
    110.         if (currentPhase == GameConstants.Phase.Start){
    111.             currentPlayer.droppedArmy = false;
    112.         }
    113.         currentPlayer.isTurn = false;
    114.         playerIndex += 1;
    115.         if (playerIndex >= players.Length){
    116.             playerIndex = 0;
    117.         }
    118.         currentPlayer = players[playerIndex];
    119.         currentPlayer.isTurn = true;
    120.         currentPlayerImage.sprite = currentPlayer.meeple;
    121.         currentPlayerName.text = currentPlayer.playerName;
    122.         //canvasController.nextPlayerButton.OnPointerUp.
    123.     }
    124.  
    125.     public void changePhase(){
    126.         switch (currentPhase){
    127.             case GameConstants.Phase.Start:
    128.                 if (playerIndex >= players.Length){
    129.                     playerIndex = 0;
    130.                 }
    131.                 currentPlayer = players[playerIndex];
    132.                 currentPlayer.isTurn = true;
    133.                 currentPlayerImage.sprite = currentPlayer.meeple;
    134.                 currentPlayerName.text = currentPlayer.playerName + "attacks";
    135.                 currentPhase = GameConstants.Phase.Combat;
    136.                 break;
    137.             case GameConstants.Phase.Combat:
    138.                 phaseText.text = "Combate";
    139.                 Debug.Log("FASE DE COMBATE. SELECCIONAR ATACANTE");
    140.                 Debug.Log("FASE DE COMBATE. SELECCIONAR DEFENSOR");
    141.                 break;
    142.             case GameConstants.Phase.Relocate:
    143.                 phaseText.text = "Ubicación";
    144.                 Debug.Log("FASE DE REUBICACIÓN. SELECCIONAR 2 CIUDADES CONECTADAS ENTRE SÍ");
    145.                 break;
    146.             case GameConstants.Phase.Reinforcement:
    147.                 phaseText.text = "Combate";
    148.                 Debug.Log("FASE DE REFUERZOS.");
    149.                 break;
    150.         }
    151.     }
    152.  
    153.     public void ActivatePhaseChange(){
    154.         phaseChange = true;
    155.     }
    156.  
    157.     public void DropArmy(){
    158.  
    159.     }
    160.  
    161. }
    162.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Country : MonoBehaviour
    7. {
    8.     public Country[] limits;
    9.     public GameController gameController;
    10.     public string countryName;
    11.     public Army army;
    12.     public Player owner;
    13.  
    14.     public bool zoomable = true;
    15.     //public GameController gameController;
    16.  
    17.     private bool highlighted = false;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         army.gameObject.SetActive(false);
    23.  
    24.         // army = Resources.Load<Army>("Prefabs/Army");
    25.         // Destroy(GetComponent<PolygonCollider2D>());
    26.         // gameObject.AddComponent<PolygonCollider2D>();
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update(){
    32.         //CheckDropArmy();
    33.         CheckClick();
    34.     }
    35.  
    36.     private void CheckClick(){
    37.         if(Input.GetMouseButtonDown(0) && highlighted){ //LEFT BUTTON CLICKED
    38.             gameController.clickOnCountry(this);
    39.         }
    40.        
    41.     }
    42.  
    43.     private void OnMouseEnter() {
    44.         highlighted = true;
    45.     }
    46.  
    47.     private void OnMouseExit() {
    48.         highlighted = false;
    49.     }
    50.  
    51. }
    52.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    You mention "once reached some phase of the game" when does this occur in your script? Seems like that would be the first step.
     
  3. Grankor

    Grankor

    Joined:
    Mar 2, 2015
    Posts:
    14
    You wouldn't be able to 'change the script' so to speak.

    From watching your video, it sounds like you want the countries to not zoom anymore.

    The simplest approach to this would be to have some kind of 'flag' triggered for this.
    (This is an idea based on what I saw)

    For example.
    If you have different phases in your game, you could create an enum.
    (The sections of the enum are completely made up, of course)

    Code (CSharp):
    1. public enum GamePhase
    2. {
    3.     Deploying,
    4.     Clicking,
    5.     HavingBreakfast
    6. }
    Somewhere you would need to keep track of your game state.
    Perhaps a static variable, or a singleton.
    Code (CSharp):
    1. pubic  static GamePhase _gamePhase  = GamePhase.Deploying;
    2.  

    Once you're done deploying your dudes, you can change the state.
    Code (CSharp):
    1. _gamePhase  = GamePhase.Clicking;
    2.  
    Then, on your zoom function

    Code (CSharp):
    1. if(_gamePhase == GamePhase.Deploying)
    2. {
    3.     //Do the zoom
    4. }
    5.  
    6. else
    7. {
    8.     //Don't zoom
    9. }
    Hope this helps :)
     
  4. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    Thank you for all replies but maybe my approacch is incorrect

    1st shot is everything clear.
    2nd shot sprite1 is zoomed.
    4th shot you can see the properties (i think that zooom.scr hanging from every country is the problem)
    3rd shot (reached state to change the form to highlight the countries).

    If you need the whole code, just tell me. Or maybe I need another approach.

    Thnaks in advance!
     

    Attached Files:

  5. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    I've finally found a solution.