0

Now I have 2 class in my project.
I want to use method class 2 in class 1.
And this code c#

Class 1

public class controlBuilding : MonoBehaviour
{
    ScaleModel cScale = new ScaleModel();

    public void Start()
    {

    }

    public void Update()
    {
        cScale.touchScaleB1();
    }

    public void OnGUI()
    {

    }
}

Class 2

public class ScaleModel : MonoBehaviour
{
    public void touchScaleB1()
    {
        if (Input.touchCount >= 2)
        {
            Touch touch1 = Input.touches[0];
            Touch touch2 = Input.touches[1];

            if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
            {
                float pinchDistance = Vector2.Distance(touch1.position, touch2.position);
                float prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition,
                                                       touch2.position - touch2.deltaPosition);
                float pinchDistanceDelta = pinchDistance - prevDistance;

                if (pinchDistanceDelta > 0)
                {
                    float mScaleFactor;
                    mScaleFactor = b1_floor1dae.transform.localScale.x;
                    mScaleFactor += 0.0004f;
                    b1_floor1dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor2dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor3dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor4dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floorTop.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_other.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                }
                else if (pinchDistanceDelta < 0)
                {
                    float mScaleFactor;
                    mScaleFactor = b1_floor1dae.transform.localScale.x;
                    mScaleFactor -= 0.0004f;
                    b1_floor1dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor2dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor3dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floor4dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_floorTop.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                    b1_other.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
                }
            }
        }
    }
}

Why I touch on screen is not event ? Where is wrong code ?

Excuse me.I'm not good English.

Thank you very much for answer.

1
  • 1
    Classes derived from Monobehaviour should not be instantiated with new. Instead use Instantiate. Have you tried adding some debugging to verify the problem is Input.touches? Commented Jul 25, 2013 at 12:34

1 Answer 1

2

If you want the ScaleModel to be only a utility class : Don't inherit it from MonoBehaviour. So you can create it with "new" keyword as a regular class.

If you want the ScaleModel to be a gameobject component : Add your both scripts to the same gameobject and change your first class like this... (by calling the GetComponent, you are searching for the ScaleModel component on the same game object)

public class controlBuilding : MonoBehaviour
{
    ScaleModel cScale;

    public void Start()
    {
        cScale = GetComponent<ScaleModel>();
    }

    public void Update()
    {
        cScale.touchScaleB1();
    }

    public void OnGUI()
    {

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What is the error you see? You should give details if you need help :)
When I pinch to zoom and zoom out is not working function touchScaleB1.But when I put code from touchScaleB1 in " public void Update" is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.