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

Hi there! I’m afraid I just got too much ahead of myself with the code I’m trying to write! :rage:
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:

using UnityEngine;
using System.Collections;

public class A : MonoBehaviour {
	
	protected int number;
	
// I made a property just to see what it can do with it

	public int Number
	{
		get{
			return number;
		}
		set{
			number = value;
		}
	}

	protected void function(int number){
		
		number = 1;
		if(Input.GetKeyDown(KeyCode.LeftShift)){
			if(Number >= 1)
				number = -1;
			else number = 1;
			
			Number += number;
			
			if( Number == 1)
				renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
			else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
		}
	}
}

SCRIPT B:

using UnityEngine;
using System.Collections;

public class B : A {
	
	// Update is called once per frame
	void Update () {
		
		//Calling Parent's Function
		function(number); 

                }
}

SCRIPT C:

using UnityEngine;
using System.Collections;

public class C : A{

	protected int wSpeed;
	protected Transform aTransform;
	
	protected void MyTranslation(Transform aTransform){
		aTransform = transform;
		aTransform.Translate(Vector3.right * -wSpeed * number * Time.deltaTime);
	}
}

SCRIPT D:

using UnityEngine;
using System.Collections;

public class D : C {
	
	
	// Use this for initialization
	void Start () {
	
		wSpeed = 30;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		MyTranslation(aTransform);
		
	}
}

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! :smile:
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.

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.

number = 1;
if(Input.GetKeyDown(KeyCode.LeftShift))
{
	if(Number >= 1)
		number = -1;
	else number = 1;


	Number += number;


	if( Number == 1)
		renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));
	else
		renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));
}

You should fix your “function”.

Why do you even use number and Number separately?

I understand what you say about the frame, but

Number += number;
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):

number = 1;

        if(Input.GetKeyDown(KeyCode.LeftShift)){

            if(Number >= 1)

                number = -1;

            else number = 1;

           

            Number += number;

           

            if( Number == 1)

                renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));

            else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));

is not the same as function(number):

number = 1;

        if(Input.GetKeyDown(KeyCode.LeftShift)){

            if(number >= 1)

                number = -1;

            else number = 1;

           

            number += number;

           

            if( number == 1)

                renderer.material.SetTextureScale("_MainTex", new Vector2(-1,1));

            else renderer.material.SetTextureScale("_MainTex", new Vector2(1,1));

Because the first returns different numbers and Numbers compared to the second one.

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.

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 :smile: