Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Some instances of the same script suddenly stop working after unity restart

Discussion in 'Scripting' started by BoomKara, Mar 28, 2024.

  1. BoomKara

    BoomKara

    Joined:
    Aug 4, 2020
    Posts:
    2
    some instances of my tech tree script stop working after unity restart, it's always the same objects. After removing the script and re-adding it everything works fine, but after i restart unity the "tech 3" and "tech 4 _ 2" stop working.
    I'm using unity 2023.2.15f1
    (sorry for ugly code xD)

    Inspector_tree.png



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. using TMPro;
    7. using System.Runtime.CompilerServices;
    8.  
    9. public class reserchable_tech : MonoBehaviour, IPointerClickHandler
    10. {
    11.  
    12.     public GameObject tech_before;
    13.     public int rp_cost = 300;
    14.     public int current_rp = 0;
    15.     public bool is_availabe_reaserch = false;
    16.     public bool is_reaserched = false;
    17.     public bool is_starter = false;
    18.     public bool is_selected = false;
    19.     private bool exec = false;
    20.     public Tech_tree tech_tree;
    21.     public resource_menager resource_menager;
    22.     public void OnPointerClick(PointerEventData pointerEventData)
    23.     {
    24.  
    25.         if (this.is_selected == true)
    26.         {
    27.             is_selected = false;
    28.             tech_tree.Something_is_reserching = false;
    29.         }
    30.         else if (this.is_selected == false)
    31.         {
    32.             if (tech_tree.Something_is_reserching == false)
    33.             {
    34.                 is_selected = true;
    35.                 tech_tree.Something_is_reserching = true;
    36.             }
    37.         }
    38.     }
    39.  
    40.     // Start is called before the first frame update
    41.     void Start()
    42.     {
    43.         tech_tree = Object.FindFirstObjectByType<Tech_tree>();
    44.         resource_menager = Object.FindFirstObjectByType<resource_menager>();
    45.         if (is_starter == true)
    46.         {
    47.             tech_before = null;
    48.         }
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.         if (tech_before != null)
    55.         {
    56.             bool pevious_is_reasarched = tech_before.GetComponent<reserchable_tech>().is_reaserched;
    57.             if (pevious_is_reasarched == true)
    58.             {
    59.                 is_availabe_reaserch = true;
    60.             }
    61.             else if (pevious_is_reasarched == false)
    62.             {
    63.                 is_availabe_reaserch = false;
    64.             }
    65.  
    66.  
    67.         }
    68.         else
    69.         {
    70.             is_reaserched = true;
    71.         }
    72.         //-------------------------------------------
    73.         if (is_reaserched == true)
    74.         {
    75.             this.gameObject.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
    76.             is_availabe_reaserch = false;
    77.             is_selected = false;
    78.         }
    79.         else if (is_availabe_reaserch == true)
    80.         {
    81.             if (is_selected == true)
    82.             {
    83.                 this.gameObject.GetComponent<Image>().color = new Color32(160, 160, 220, 255);
    84.             }
    85.             else
    86.             {
    87.                 this.gameObject.GetComponent<Image>().color = new Color32(160, 160, 160, 255);
    88.             }
    89.  
    90.  
    91.         }
    92.         else
    93.         {
    94.             this.gameObject.GetComponent<Image>().color = new Color32(100, 100, 100, 255);
    95.             is_selected = false;
    96.         }
    97.        
    98.         if (Time.time > resource_menager.nextActionTime)
    99.         {
    100.             if (is_selected == true)
    101.             {
    102.                 Debug.Log(this.gameObject.name + " time");
    103.                 current_rp = current_rp + resource_menager.rp_gain_per_day;
    104.                 Debug.Log(this.gameObject.name + " is reserching");
    105.             }
    106.             if (current_rp >= rp_cost)
    107.             {
    108.  
    109.                 if (exec == false)
    110.                 {
    111.                     current_rp = rp_cost;
    112.                     is_reaserched = true;
    113.                     is_selected = false;
    114.                     is_availabe_reaserch = false;
    115.                     tech_tree.Something_is_reserching = false;
    116.                     exec = true;
    117.                 }
    118.             }
    119.            
    120.         }
    121.        
    122.     }
    123. }
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    232
    You should try to describe what "not working" is in your case

    Odds are it has something to do with script execution order or just logic mistake

    Check out some of the other threads in this forum, there should be some guide-like answers on how to debug by Kurt-Dekker

    (I am writing this to just let you know why most people skipped answering your question, I'll probably wont be able to spend any more time other than just telling you to learn to debug)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,060
    Generally don't do stuff like this:

    or this:

    Keep in mind that using GetComponent<T>() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

    Every time you add a dot or use a Find() or Get() method, you increase the possible places your code can fail.

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    "Stop playing 'Where's GameWaldo' and drag it in already!"
     
    Ryiah likes this.
  4. BoomKara

    BoomKara

    Joined:
    Aug 4, 2020
    Posts:
    2
    Thanks for the replies, but i have already resolved the problem.
    (this line was the trouble maker "if (Time.time > resource_menager.nextActionTime)" )