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

Biting off more than I can chew, need some help!

Discussion in 'Scripting' started by esitoinatteso, Oct 17, 2013.

  1. esitoinatteso

    esitoinatteso

    Joined:
    Sep 23, 2013
    Posts:
    26
    Hi there! I'm afraid I just got too much ahead of myself with the code I'm trying to write! :mad:
    Could you help me understand better this issue that's driving me crazy?
    Please, note that this problem originates from the fact that I'm a beginner self-taught programmer... could you please be exhaustive in your replies and even avoid to feed me with plain code? Thanks!

    I got 4 different scripts:
    A, B, C, D.

    A is the manager, B and C are its childs, and D is child of C ( which means that it's also child of A, right?).

    I've made a property in A that organises an int " number".
    I've also made a protected function " function(int number)" that, when called, changes this number and do some other things.

    I'm running function from the Update of script B.
    Problem is that when I try to access the number modified by B from C and D I get a value which is not right.

    Here are portions of my code:
    SCRIPT A:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class A : MonoBehaviour {
    5.    
    6.     protected int number;
    7.    
    8. // I made a property just to see what it can do with it
    9.  
    10.     public int Number
    11.     {
    12.         get{
    13.             return number;
    14.         }
    15.         set{
    16.             number = value;
    17.         }
    18.     }
    19.  
    20.     protected void function(int number){
    21.        
    22.         number = 1;
    23.         if(Input.GetKeyDown(KeyCode.LeftShift)){
    24.             if(Number >= 1)
    25.                 number = -1;
    26.             else number = 1;
    27.            
    28.             Number += number;
    29.            
    30.             if( Number == 1)
    31.                 renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
    32.             else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
    33.         }
    34.     }
    35. }
    SCRIPT B:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class B : A {
    5.    
    6.     // Update is called once per frame
    7.     void Update () {
    8.        
    9.         //Calling Parent's Function
    10.         function(number);
    11.  
    12.                 }
    13. }
    SCRIPT C:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class C : A{
    5.  
    6.     protected int wSpeed;
    7.     protected Transform aTransform;
    8.    
    9.     protected void MyTranslation(Transform aTransform){
    10.         aTransform = transform;
    11.         aTransform.Translate(Vector3.right * -wSpeed * number * Time.deltaTime);
    12.     }
    13. }
    SCRIPT D:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class D : C {
    5.    
    6.    
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.         wSpeed = 30;
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.        
    17.         MyTranslation(aTransform);
    18.        
    19.     }
    20. }
    21.  
    What happens is that script B is capable of flip its object's texture, script D doesn't move properly instead, because sometime number = 0 to him or something like that.
    I'm sorry I've posted you a generic code but my originals are messy and full of comments.
    I also tried a lot of different methods to get my hand to number's proper value but I couldn't manage to do it.

    Right now I'm thinking about deleting all this code and write it in a simpler way... but if I do so I wouldn't learn.
    Would you please help me learning how to handle this mess?
    Many thanks for your time! :D
    P.S.: I preferred to post a new Thread here on the Forum before asking a question on UA, I felt I couldn't be specific enough with this problem to post a question there.
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    number and Number will after calling this function, always be 1 except when the user presses the LeftShift key down, after that the next frame will set number to 1. So in case the user presses the LeftShift key, number and Number will be set to -2. This seems to be not what you want at all.
    Code (csharp):
    1.  
    2. number = 1;
    3. if(Input.GetKeyDown(KeyCode.LeftShift))
    4. {
    5.     if(Number >= 1)
    6.         number = -1;
    7.     else number = 1;
    8.  
    9.  
    10.     Number += number;
    11.  
    12.  
    13.     if( Number == 1)
    14.         renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
    15.     else
    16.         renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
    17. }
    18.  
    19.  
    You should fix your "function".

    Why do you even use number and Number separately?
     
  3. esitoinatteso

    esitoinatteso

    Joined:
    Sep 23, 2013
    Posts:
    26
    I understand what you say about the frame, but

    Code (csharp):
    1. Number += number;
    2. print ("Number = " +Number + " number = " + number);
    Gives only 1 or -1 as numbers and 0 and 1 as Numbers, it won't return anything else.
    Instead, when you ask for the same print in B, right after function is called, Number and number are always equal ( or 0s or 1s).

    I don't know why I keep those separated, Number is a property instead of a simple variable, fact is that you don't get the same result if you change Number with number... if you do that you then get the result you talk about.
    My problem should be elsewhere, I suspect in Script C when i call number from there.

    Just to be clearer...
    Number and number are not the same thing.
    Number manages to be always 1 when number is either 1 or -1.
    I don't know if i need a property like Number in this case, but since I have it I use it in that way and I can assure you that function(number):
    Code (csharp):
    1. number = 1;
    2.  
    3.         if(Input.GetKeyDown(KeyCode.LeftShift)){
    4.  
    5.             if(Number >= 1)
    6.  
    7.                 number = -1;
    8.  
    9.             else number = 1;
    10.  
    11.            
    12.  
    13.             Number += number;
    14.  
    15.            
    16.  
    17.             if( Number == 1)
    18.  
    19.                 renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
    20.  
    21.             else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
    is not the same as function(number):
    Code (csharp):
    1. number = 1;
    2.  
    3.         if(Input.GetKeyDown(KeyCode.LeftShift)){
    4.  
    5.             if(number >= 1)
    6.  
    7.                 number = -1;
    8.  
    9.             else number = 1;
    10.  
    11.            
    12.  
    13.             number += number;
    14.  
    15.            
    16.  
    17.             if( number == 1)
    18.  
    19.                 renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
    20.  
    21.             else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
    Because the first returns different numbers and Numbers compared to the second one.
     
  4. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Oops, my fault, I missed that the argument/parameter of your function was named number too. Maybe thats the point you need to fix first. Dont call arguments the same as members, as you can see this leads to confusion.
     
  5. esitoinatteso

    esitoinatteso

    Joined:
    Sep 23, 2013
    Posts:
    26
    Hehe you are right! I've to fix that to begin with in my code... still, I'm unpleased with the way my code is reacting at my efforts; I've managed to make it perform a lot of different behaviours except for the one I'm looking for, which is to invert wSpeed when the user has pushed shift!
    Thanks anyway for your reply :D