Search Unity

calling function from 2 other functions getting different results

Discussion in 'Scripting' started by Tempus_G, May 8, 2021.

  1. Tempus_G

    Tempus_G

    Joined:
    May 12, 2015
    Posts:
    53
    I am placing objects during game play from a menu to place in scene.
    I can also select objects from the scene to adjust objects already in scene.

    First check is to see if an object from the menu is already in the scene from a list.
    Code (CSharp):
    1. string[] objects = { "object 1(Clone)", "object 5(Clone)", "object 10(Clone)" };
    2.  
    3.         if (!GameObject.Find(ObjectPrefabSelection[ObjectPrefab].name + "(Clone)"))
    4.         {
    5.             currentObject = Instantiate(ObjectPrefabSelection[ObjectPrefab]);
    6.             Scalable();
    7.         }
    8.         else
    9.         if (GameObject.Find(ObjectPrefabSelection[ObjectPrefab].name + "(Clone)") != Array.Exists(objects, element => element == ObjectPrefabSelection[ObjectPrefab].name + "(Clone)"))
    10.         {
    11.             currentObject = Instantiate(ObjectPrefabSelection[ObjectPrefab]);
    12.             Scalable();
    13.         }
    When selecting an object from the scene
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.             {
    3.                 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, ObjectlayerMask)) // 1 << LayerMask.NameToLayer("IsClickable")
    4.                 {
    5.                     if (hit.transform != null)
    6.                     {
    7.                         if (!hit.transform.parent)
    8.                         {
    9.                             currentObject = hit.transform.gameObject;
    10.                             Scalable();
    11.                         }
    12.                         else
    13.                         {
    14.                             currentObject = hit.transform.parent.gameObject;
    15.                             Scalable();
    16.                         }
    17.                     }
    18.                 }
    19.             }
    And from both, I am calling this code to check a separate list to see if it is a scalable object.
    Code (CSharp):
    1. public void Scalable()
    2.     {
    3.         string[] selectables = { "object 1(Clone)", "object 8(Clone)" };
    4.         if (currentObject != Array.Exists(selectables, element => element == ObjectPrefabSelection[ObjectPrefab].name + "(Clone)"))
    5.         {
    6.             Debug.Log("FALSE " + currentObject);
    7.             scalable = false;
    8.         }
    9.         else
    10.         {
    11.             Debug.Log("TRUE " + currentObject);
    12.             scalable = true;
    13.         }
    14.     }
    The problem that I am having is that the "Scalable" code is not running correctly when I select objects from the scene.
    Am I missing something simple?

    I have tried calling the "Scalable" code from different places in the code, but same results.
     
    Last edited: May 9, 2021
  2. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    you've done some interesting stuff here...

    GameObject.Find(ObjectPrefabSelection[ObjectPrefab].name + "(Clone)")
    and
    Array.Exists(objects, element => element == ObjectPrefabSelection[ObjectPrefab].name + "(Clone)")
    will never be equal as the first one returns a gameobject while the second one returns a bool. here too
    currentObject != Array.Exists(selectables, element => element == ObjectPrefabSelection[ObjectPrefab].name + "(Clone)")
    you try to compare an object to a bool end here
    !hit.transform.parent
    you try to check if a gameobject is true or false
     
  3. Tempus_G

    Tempus_G

    Joined:
    May 12, 2015
    Posts:
    53
    So what is the problem? If object of given name is true or false..... do something.
    I don't have any issue with the code that I can see, just the behaviour when called from different places. But I am open to ideas.
     
  4. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    but an object is not true or false, it either exists or not, it has a specific name or not, it has a tag or not but its not. a car can be red or white but true?

    Code (CSharp):
    1. if (!GameObject.Find(ObjectPrefabSelection[ObjectPrefab].name + "(Clone)"))
    this check should look like
    Code (CSharp):
    1. if (GameObject.Find(ObjectPrefabSelection[ObjectPrefab].name + "(Clone)") != null)
     
  5. Tempus_G

    Tempus_G

    Joined:
    May 12, 2015
    Posts:
    53
    Dont you mean " = null" ? And it is just short hand for the same thing.
    And if I am looking for a red car from a list, then red can be true or false.
    Not sure where you are going with this, as it is not changing or fixing my problem. But I am open to see if one implementation of code works better than another if it may impact the outcome.
     
  6. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    no, red cant be true or false
    color can be red or blue but not true or false
     
  7. Tempus_G

    Tempus_G

    Joined:
    May 12, 2015
    Posts:
    53
    Right. I have found something.
    Although I am using the same objects, the referencing is different. An oversite on my part.
    So a little change was made and is working.

    Here is the updated code
    Code (CSharp):
    1. public void Scalable()
    2.     {
    3.      
    4.         string currentObjectName = currentObject.name;
    5.         string[] selectables = { "object 1(Clone)", "object 8(Clone)" };
    6.         if ((currentObject != Array.Exists(selectables, element => element == currentObjectName)))
    7.            {
    8.                Debug.Log("FALSE " + currentObject);
    9.                scalable = false;
    10.            }
    11.            else
    12.            {
    13.                Debug.Log("TRUE " + currentObject);
    14.                scalable = true;
    15.            }
    16.         }
    17.     }
     
    Last edited: May 9, 2021