Home>

We are making a puzzle game that fits shapes in unity.
When the shape fits the shape, I wrote the script using the bool value so that it could not be moved, but it does not change even if the condition is satisfied.

Below script

Script with method written

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMethodScript: MonoBehaviour
{
    public int goalcount;


    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        Debug.Log(goalcount);
    }
     public void grphicmethod(string name1, string name2, float buzzle, Vector3 point, bool fit)
    {
        if (Input.GetMouseButton(0))
        {
            point = Camera.main.WorldToScreenPoint(transform.position);
            Ray ray = new Ray();
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, Mathf.Infinity);

            if (hit.collider.gameObject.CompareTag(name1))
            {

                point = Camera.main.WorldToScreenPoint(transform.position);
                Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, point.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint);
                hit.collider.gameObject.transform.position = currentPosition;
            }
        }

        if (GameObject.FindWithTag(name1).transform.position.x >GameObject.FindWithTag(name2).transform.position.x-buzzle
       &&gameObject.FindWithTag(name1).transform.position.x<GameObject.FindWithTag(name2).transform.position.x + buzzle
       &&gameObject.FindWithTag(name1).transform.position.y >GameObject.FindWithTag(name2).transform.position.y-buzzle
       &&gameObject.FindWithTag(name1).transform.position.y<GameObject.FindWithTag(name2).transform.position.y + buzzle)
        {
            fit = false;
            Debug.Log("OK");GameObject.FindWithTag(name1).transform.position = GameObject.FindWithTag(name2).transform.position;
        }


    }
}

Script that runs the method

using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
public class GameControllerScript: MonoBehaviour
{
    public Vector3 screenPoint;
    public GameObject tr, cir, squ, gen, st, trgoal, cirgoal, squgoal, gengoal, stgoal;
    private int a, b, c;
    public bool trfit, genfit, cirfit, squfit, stfit;
    public GameObject[] grphics;
    public GameObject[] goal;
    private GameMethodScript gms;
    // Start is called before the first frame update
    void Start()
    {
         grphics = new GameObject[]{ tr, cir, squ, gen, st };
         goal = new GameObject[] {trgoal, cirgoal, squgoal, gengoal, stgoal };
        gamestart();
        gms = GameObject
            .FindWithTag("GameMethod")
            .GetComponent<GameMethodScript>();

    }

    void Update()
    {

        Debug.Log(trfit);
        if (trfit)
        {
            gms.grphicmethod("tr", "trgoal", 0.2f, screenPoint, trfit);
        }
        gms.grphicmethod("gen", "gengoal", 0.2f, screenPoint,genfit);
        gms.grphicmethod("cir", "cirgoal", 0.2f, screenPoint,cirfit);
        gms.grphicmethod("squ", "squgoal", 0.2f, screenPoint, squfit);
        gms.grphicmethod("st", "stgoal", 0.2f, screenPoint,stfit);

    }
    public void gamestart()
    {trfit = true;
        genfit = true;
        cirfit = true;
        squfit = true;
        stfit = true;
        a = Random.Range(0, 5);
        do
        {
            b = Random.Range(0, 5);
        }
        while (a == b);
        do
        {
            c = Random.Range(0, 5);
        }
        while (a == c || b == c);
        GameObject[] goal2 = {goal[a],

 goal[b],

 goal[c] };
        goal2 = goal2.OrderBy(i =>System.Guid.NewGuid()).ToArray();

        Instantiate(grphics[a],

 new Vector3(-5f, -2.5f, 0f), transform.rotation);
        Instantiate(grphics[b],

 new Vector3(0f, -2.5f, 0f), transform.rotation);
        Instantiate(grphics[c],

 new Vector3(5f, -2.5f, 0f), transform.rotation);
        Instantiate(goal2[0],

 new Vector3(-5f, 2f, 0f), transform.rotation);
        Instantiate(goal2[1],

 new Vector3(0f, 2f, 0f), transform.rotation);
        Instantiate(goal2[2],

 new Vector3(5f, 2f, 0f), transform.rotation);
    }

}

In the method
DebugDebug.Log("OK");
Is moving.

Please tell me.

  • Answer # 1

    It was solved by adding "out".

    public void grphicmethod(string name1, string name2, float buzzle, Vector3 point, out bool fit)
    /* method definition */
    gms.grphicmethod("tr", "trgoal", 0.2f, screenPoint, out trfit);
    /* method execution part */