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