Home > other >  How to find the script of a specific gameobject from a series of gameobjects with the same script in
How to find the script of a specific gameobject from a series of gameobjects with the same script in

Time:03-15

I currently have two gameobjects with the same script. The script have button.onClick.AddListener(MethodInTheSameScript). What the method does is calling another method in another script that needs a variable that is inside the first script, which is different in each of the 2 gameobjects (a json file). If i now press the button, both the gameobjects call the method, but since the other script can only use one json file, they now both use the same one. But, if i move the onClick outside of the script, so that it only listen once, i can only reference one of their scripts, so they still both end up using the same json file. I need a way to either stop the gameobject im not interacting with to listen the the button or to find the script of the specific gameobject im interacting with. I also cant use a simple if statement because the script that makes the user interact with one of the gameobjects its the same in both of them.

[SerializeField] private TextAsset inkJSON;
[SerializeField] private Button button;

private void Start()
{
    
    button.onClick.AddListener(DisplayDialogue);
}
public void DisplayJsonText()
{
    
    OtherScript.GetInstance().OtherScriptMethod(inkJSON);
}

CodePudding user response:

Ok it's probabably dumb but i found a solution. Basically i made a static string that store a gameobject name when is interacted with (in my case, when is clicked on). Then when you click on the button, the script looks for the clicked gameobject script using it's name and then uses it.

class Main

public static string gameObjectName;

private void Start()
{
    button.onClick.AddListener(TriggerOtherScript);
}


  void TriggerOtherScript()
{
    FindGameObjectClicked();
    otherScript.OtherScriptMethod();
}

 private void FindGameObjectClicked()
{
    otherScript = GameObject.Find(gameObjectName).GetComponent<otherScript>();
}

class GameObjectScript
  public void onm ouseDown() 
   {
   
        Main.gameObjectName = this.gameObject.name;
        
    }

CodePudding user response:

Try adding a condition to listen for the mouseinput. Something like a hover boolean for the gameObject class for example:

class gameObject
{
    public int X, Y, Width, Height;

    public static bool Hover(float[] mouseInput)
    {
        if(mouseInput.X > this.X && mouseInput.X < this.X   this.Width
            && mouseInput.Y > this.Y && mouseInput.Y < this.Y   this.Height)
        {
            return true;
        }
        return false;
    }
}

Then you can simply check for the hover before executing the code:

public void Start()
{
    if(this.Hover(mouseInput))
    {
         button.onClick.AddListener(DisplayDialogue);
    }
}

That way only the gameobject you're actually hovering will activate when you click the mouse. Not the most efficient way, I am sure (I'm a beginner with c#) , but it should work regardless of how you've implemented the rest of the code.

  • Related