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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroying and not destroying (Not Solved)

Discussion in 'Scripting' started by Rom210, May 6, 2015.

  1. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    I'm currently have two scripts. one with the variable m which equals number 1-4. i got another script with t which t also equals 1-4. m depends on what the player pushes. and t is random. I want to make a separate script that allows me to see if m and t equal each other. if they don't i want a game object to be destroyed. I tried doing this myself. but for some reason it always say t and m equal each other. here's the script:

    Code (CSharp):
    1. public class Killer : MonoBehaviour {
    2.  
    3.     public ColorChanger m1;
    4.     public ChangeBulletColor p1;
    5.  
    6.     // Use this for initialization
    7.     void OnCollisionEnter(Collision col) {
    8.         if (m1.m == p1.t){
    9.             //+1 point
    10.             print ("It's equal")
    11.         }
    12.         if (m1.m != p1.t) {
    13.             //kill
    14.             print ("kill")
    15.         }
    16.     }
    17. }
    Btw I'm using c#. and I am new to coding. here's whats going on in m1
    Code (CSharp):
    1. public class ColorChanger : MonoBehaviour {
    2.     public int m;
    3.     public void Update () {
    4.  
    5.     if(Input.GetKeyDown(KeyCode.A)){
    6.             m=1;
    7.         }
    8.         if(Input.GetKeyDown(KeyCode.S)){
    9.             m=2;
    10.         }
    11.         if(Input.GetKeyDown(KeyCode.D)){
    12.             m=3;
    13.         }
    14.         if(Input.GetKeyDown(KeyCode.F)){
    15.             m=4;
    16.         }
    17.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how is "Killer" getting the values for m1 and p1? linked in the inspector?

    add some debug lines so you can see what the values and references are, i.e.
    Code (csharp):
    1.  
    2. Debug.Log("m1 name: " + m1.name + " m = " + m1.m + " p1 name:" + p1.name + " p = " + p1.t);
    3.  
    as the first line in OnCollisionEnter(...)
     
    dterbeest likes this.
  3. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    well, p seems to be working perfectly. the problerm is m

    m1 name: BoxBottom m = 0 p1 name:Bullet(Clone) p = 4

    no matter what i do, m always = 0. do you see any scripting errors in it above? if so what?
     
  4. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    anyone who can solve this would be much help :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Step 1: put a Debug.Log() call inside your update loop, make sure that's being called.

    Step 2: put a Debug.Log() call inside your KeyCode.A call, make sure that's being called (when you press A obviously)
     
  6. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    As long as these scripts are linked in the inspector, in other words you dragged the ColorChanger script onto the m1 variable, and both objects are attached to an active GameObject then there is absolutely no reason why m would always return 0.

    Possible Causes:
    1. Make sure both scripts are attached to an active GameObject.
    2. Are the scripts linked in the inspector?
    2. Are you even pressing the A, S, D, or F keys?

    *NOTE* You have two if statements in your Killer class; this was not necessary and the same effect could have been done with an if else statement. You're just checking if something is equal or not; if, it is, else, it isn't.

    Code (CSharp):
    1.  
    2. public class Killer : MonoBehaviour {
    3.  
    4.         public ColorChanger m1;
    5.         public ChangeBulletColor p1;
    6.  
    7.         // Use this for initialization
    8.         void OnCollisionEnter(Collision col) {
    9.             if (m1.m == p1.t){
    10.                 //+1 point
    11.             } else {
    12.                 //kill
    13.             }
    14.         }
    15.     }
    16.  
    Code (CSharp):
    1.  
    2. public class ColorChanger : MonoBehaviour {
    3.     public int m;
    4.     public void Update () {
    5.     if(Input.GetKeyDown(KeyCode.A)){
    6.             m=1;
    7.         }
    8.         if(Input.GetKeyDown(KeyCode.S)){
    9.             m=2;
    10.         }
    11.         if(Input.GetKeyDown(KeyCode.D)){
    12.             m=3;
    13.         }
    14.         if(Input.GetKeyDown(KeyCode.F)){
    15.             m=4;
    16.         }
    17.      }
    18. }
     
  7. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    I believe i found the error. for some reason it wont allow me to drag the actual game object to the inspector.but it will let me put an exact copy of it in my assets into the inspector. I don;t understand why.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    The inspector of what? I think you're inspecting something in the Project list, not the Hierarchy list.

    Be sure you're selecting and setting things up in the Hierarchy. (It's possible to do this to some extent in Project too, when working with prefabs, but at this stage I think that would only lead you to confusion.)
     
    Kiwasi and hamsterbytedev like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    At the risk of creating the aforementioned confusion: Items in the project view (ie the asset folder) can only have references to other items in the project view. Items in the hierarchy can have references to both the hierarchy and the project view.

    Only items in the hierarchy view are actually "in the game", in most cases stuff in the project view can't actually do anything.
     
    hamsterbytedev and JoeStrout like this.