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

Cant change variable?

Discussion in 'Scripting' started by kaysteinhoff2003, Jul 7, 2020.

  1. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    Hi so in my script I'm tryong to customize my character through pieces and for selecting them I have chosen to use integers. Strangely I can not change them although I'm refrencing my player.

    Here's my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerBody : MonoBehaviour
    6. {
    7.    
    8.     public bool male;
    9.     public bool female;
    10.    
    11.     public int Hair;
    12.     public int Head;
    13.     public int Torso;
    14.     public int Legs;
    15.    
    16.     public GameObject Editor;
    17.     public GameObject Player;
    18.    
    19.     void Start()
    20.     {
    21.         DontDestroyOnLoad(this.gameObject);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         Editor = GameObject.FindGameObjectWithTag("Editor");
    27.         Player = GameObject.FindGameObjectWithTag("Player");
    28.        
    29.         if(Editor == null)
    30.         {
    31.             Player.gameObject.GetComponent<PlayerCustomise>().Male = male;
    32.             Player.gameObject.GetComponent<PlayerCustomise>().Female = female;
    33.             Player.gameObject.GetComponent<PlayerCustomise>().Hair = Hair;
    34.             Player.gameObject.GetComponent<PlayerCustomise>().Head = Head;
    35.             Player.gameObject.GetComponent<PlayerCustomise>().Torso = Torso;
    36.             Player.gameObject.GetComponent<PlayerCustomise>().Leg = Legs;
    37.         }
    38.        
    39.         if(Editor != null)
    40.         {
    41.             male = Player.gameObject.GetComponent<PlayerCustomise>().Male;
    42.             female = Player.gameObject.GetComponent<PlayerCustomise>().Female;
    43.            
    44.             Hair = Player.gameObject.GetComponent<PlayerCustomise>().Hair;
    45.             Head = Player.gameObject.GetComponent<PlayerCustomise>().Head;
    46.             Torso = Player.gameObject.GetComponent<PlayerCustomise>().Torso;
    47.             Legs = Player.gameObject.GetComponent<PlayerCustomise>().Leg;
    48.            
    49.             Player.gameObject.GetComponent<PlayerCustomise>().ChangePieces();
    50.         }
    51.     }
    52. }
     
  2. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    Edit:
    I found my mistake as soon as I created this Thread so please don't bother yourself with answers :)

    also if anyone is interested this is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerBody : MonoBehaviour
    6. {
    7.    
    8.     public bool male;
    9.     public bool female;
    10.    
    11.     public int Hair;
    12.     public int Head;
    13.     public int Torso;
    14.     public int Legs;
    15.    
    16.     public GameObject Editor;
    17.     public GameObject Player;
    18.    
    19.     void Start()
    20.     {
    21.         DontDestroyOnLoad(this.gameObject);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         Editor = GameObject.FindGameObjectWithTag("Editor");
    27.         Player = GameObject.FindGameObjectWithTag("Player");
    28.        
    29.         if(Editor == null)
    30.         {
    31.             Player.gameObject.GetComponent<PlayerCustomise>().Male = male;
    32.             Player.gameObject.GetComponent<PlayerCustomise>().Female = female;
    33.             Player.gameObject.GetComponent<PlayerCustomise>().Hair = Hair;
    34.             Player.gameObject.GetComponent<PlayerCustomise>().Head = Head;
    35.             Player.gameObject.GetComponent<PlayerCustomise>().Torso = Torso;
    36.             Player.gameObject.GetComponent<PlayerCustomise>().Leg = Legs;
    37.            
    38.             Player.gameObject.GetComponent<PlayerCustomise>().ChangePieces();
    39.         }
    40.        
    41.         if(Editor != null)
    42.         {
    43.             male = Player.gameObject.GetComponent<PlayerCustomise>().Male;
    44.             female = Player.gameObject.GetComponent<PlayerCustomise>().Female;
    45.            
    46.             Hair = Player.gameObject.GetComponent<PlayerCustomise>().Hair;
    47.             Head = Player.gameObject.GetComponent<PlayerCustomise>().Head;
    48.             Torso = Player.gameObject.GetComponent<PlayerCustomise>().Torso;
    49.             Legs = Player.gameObject.GetComponent<PlayerCustomise>().Leg;
    50.         }
    51.     }
    52. }
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I'm going to comment that you should move the GameObject.FindGameObjectWithTag calls into Start. Otherwise, every frame you're finding those objects which has no value and is just wasted processing power. If you can't drag and drop in the inspector, then you only need to search for them once. Otherwise, you can just assign the values when you need to (when they are instantiated for example).

    Actually, I'm not even sure if the entire Update method is necessary. Do you need to assign the values over and over again like that?
     
  4. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    I could move the lines 26 - 39 in the start method but the rest I need in the Update because I handle the customisation of the character in an other script so I need it there but thanks for the advice! :D
     
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    764
    Store the PlayerCustomise as local reference, GetComponent() is also expensive.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Even if you are changing it in another script, you still don't need this in Update. Update is designed for things that need to happen every frame. If a person makes a change to hair for example, simply call a method to update the hair. It's a pretty simple setup. But, if nothing else, at least do what @runner78 suggests if you don't know how to set it up the way I'm suggesting.