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

Scripts suddenly not working

Discussion in 'Scripting' started by llewellynathan, Oct 15, 2019.

Thread Status:
Not open for further replies.
  1. llewellynathan

    llewellynathan

    Joined:
    Sep 6, 2019
    Posts:
    10
    This morning I discovered that almost all of my scripts in a small project I have been working on have stopped working. They were working yesterday just fine and I didn't change anything in Unity between when they were working and this morning when I discovered the scripts to no longer be working. What could have possibly caused this?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    What is not working?
    Post your script.
    Remember to use Code Tag.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,382
    "Not working" could mean anything. Do you have an error message?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Gonna need more information.

    Do you have any compile errors in the console?
    Look at the objects in the scene. Do you still see your scripts in the inspector?
    Are the script files themselves still present?
     
  5. llewellynathan

    llewellynathan

    Joined:
    Sep 6, 2019
    Posts:
    10
    I get no errors in my console of any kind. All of the scripts are still attached and any object a script was referencing is still showing in the inspector. Here's one of my scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Fire : MonoBehaviour
    6. {
    7.     public ParticleSystem flames;
    8.  
    9.     private void OnCollisionEnter(Collision collision)
    10.     {
    11.         Debug.Log("Collision!");
    12.         flames = collision.gameObject.GetComponent<ParticleSystem>();
    13.         flames.Play(true);
    14.        
    15.     }
    16. }
    Just yesterday the particle system would play when the collision occurred but now nothing happens. Here's another script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Explode : MonoBehaviour
    6. {
    7.  
    8.     private int counter = 0;
    9.     public GameObject explosion;
    10.     public Rigidbody ball;
    11.  
    12.     private void OnCollisionStay(Collision collision)
    13.     {
    14.         Debug.Log(collision.gameObject.name + " rolled onto " + gameObject.name + " counter + " + counter);
    15.  
    16.         counter += 1;
    17.  
    18.         if (counter == 30)
    19.         {
    20.             Destroy(gameObject, .4f);
    21.             Instantiate(explosion);
    22.             explosion.SetActive(true);
    23.             ball.AddForce(Vector3.up * 500f);
    24.  
    25.         }
    26.  
    27.     }
    I opened a new scene and was only able to get this script to work after removing and reattaching the explosion object and Rigidbody. Is it possible that my original scene was corrupted? One day it was working fine, the next my scripts no longer worked as they had. No errors of any kind to indicate what could be wrong.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Chances are, you haven't saved scene.
     
  7. llewellynathan

    llewellynathan

    Joined:
    Sep 6, 2019
    Posts:
    10
    That doesn't seem to be the problem. Even after saving the scene it still doesn't work. This script was executing as expected yesterday, today nothing happens. No errors, no Deb.Log message in the console, nothing.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    If you make a brand new script that does something trivial and detectable (e.g. Debug.Log in Start), and put that in your scene, does that work?
     
  9. SnakeBitten

    SnakeBitten

    Joined:
    Mar 15, 2021
    Posts:
    1
    I am having the same problem. One of my scripts wasn't working so I closed Unity and when I came back none of them were working anymore, even though they were unaltered.
     
  10. cofcof

    cofcof

    Joined:
    Sep 17, 2013
    Posts:
    10
    I have been having the same problem same code in test scenes works fine in my main scene does not work with any errors code has not changed works fine in the test scene the same prefab in the main scene nothing tested back to back creat a new scene drop in prefab works close unity and start it again stops working. Project corrupt because it keeps happening what recreate everything what can I port and not port to new build.
     
  11. cofcof

    cofcof

    Joined:
    Sep 17, 2013
    Posts:
    10
    yeah its any scene I close on as well it happens
     
  12. cofcof

    cofcof

    Joined:
    Sep 17, 2013
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6.  
    7. public class Door : MonoBehaviour
    8. {
    9.     [SerializeField] GameObject[] switches = new GameObject[] { null };
    10.     private string[] reqSwitches;
    11.  
    12.     private int index;
    13.     public string key;
    14.    
    15.     Vector3 startPos;
    16.     Vector3 lerpPos;
    17.  
    18.     // Start is called before the first frame update
    19.  
    20.     private bool CheckSwitches()
    21.     {
    22.         bool output = false;
    23.  
    24.         for(int i = 0; i < switches.Length; i++)
    25.         {
    26.             GameObject curGO = switches[i];
    27.             string key = curGO.GetComponent<Switch_Pressure>().key;
    28.             int index = SwitchData.FindKeyIndex(key);
    29.  
    30.             int lastSwitchIndex = Mathf.Clamp(i, 0, switches.Length - 1);
    31.             GameObject lastGO = switches[lastSwitchIndex];
    32.             string lastKey = lastGO.GetComponent<Switch_Pressure>().key;
    33.             int lastIndex = SwitchData.FindKeyIndex(key);
    34.  
    35.             if(!SwitchData.switches[lastIndex].onOff && SwitchData.switches[this.index].onOff)
    36.             {
    37.                 output = false;
    38.                 SwitchData.switches[lastIndex].onOff = false;
    39.                 SwitchData.switches[this.index].onOff = false;
    40.                 break;
    41.             }
    42.             else
    43.             {
    44.                 output = SwitchData.switches[this.index].onOff;
    45.             }
    46.         }
    47.  
    48.         return output;
    49.     }
    50.  
    51.     private void Start()
    52.     {
    53.         startPos = transform.position;
    54.         lerpPos = new Vector3(transform.position.x, transform.position.y + 12, transform.position.z);
    55.     }
    56.  
    57.     private void SetDoorsState(bool openClose)
    58.     {
    59.         if(openClose)
    60.         {
    61.             Debug.Log("Door Open");
    62.             transform.position = Vector3.Lerp(transform.position, lerpPos, Time.deltaTime * 2);
    63.         }
    64.         else
    65.         {
    66.             transform.position = Vector3.Lerp(transform.position, startPos, Time.deltaTime * 2);
    67.         }
    68.     }
    69.  
    70.     [SerializeField] bool openClose = false;
    71.  
    72.     // Update is called once per frame
    73.     void FixedUpdate()
    74.     {
    75.         Debug.Log("Door Open " + name);
    76.  
    77.         if (switches[0] != null)
    78.         {
    79.             SetDoorsState(openClose);
    80.         }
    81.     }
    82. }
    83. /*everything else its connected to works switches change material color to green and the database updates can see the switches data in the save file and everything the save file even logs the door opening.*/
     
  13. cofcof

    cofcof

    Joined:
    Sep 17, 2013
    Posts:
    10
    Someone can help would be great feeling like maybe I am making a mistake because results are inconsistent I am trying to build a switch network so if they are not hit in right order and if other switches are hit that others will turn off.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    You're posting random comments and random code to a thread that is over two years old.

    Please instead choose to set yourself up for success on the forums and START YOUR OWN FRESH POST, it's FREE.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
Thread Status:
Not open for further replies.