In Unity, C#, you want to keep the value of the object you clicked and use other methods.

Asked 1 years ago, Updated 1 years ago, 37 views

C# I'm a beginner.
What you want to do is
Click any 3D object → Delete button is displayed.
Click Delete Button → The 3D object is deleted.
That's what it is.

Unity has EventTrigger configuration for 3D objects and buttons.
Currently, when you click on a 3D object, a button appears and the value of the 3D object is retrieved.
I would like to use the value of the 3D object in the next method to delete it.
Thank you for your cooperation.

public class ButtonController:MonoBehavior
{
  // Click on the 3D object and the following methods will be called:
  // A button appears asking if you want to delete the object.
  public void OpenDelPanel()
  {
    GameObjectSelobj=this.gameObject;
    Debug.Log (Selobj); // Unity console displays the name of the 3D object

    GameObject Pop_Delete = GameObject.Find("Pop_Delete");
    Pop_Delete.SendMessage("PopDelStartAnimation");
  }


  // I want to delete the 3D object after clicking the "Delete Button" displayed on the screen above.
  public void DelObj()
  {
    // I'd like to put the value of the 3D object I clicked on in "***"...
    Destroy(***);
  }
}

c# unity3d

2022-09-30 14:04

1 Answers

I feel that the variable name defined in the method could only be used in that method.Therefore, you may want to define the variable name in the field first.

public class ButtonController:MonoBehavior
{
 GameObject Pop_Delete;
 // like this

 public void OpenDelPanel()
 {
  GameObjectSelobj=this.gameObject;
  Debug.Log (Selobj); // Unity console displays the name of the 3D object

  GameObject Pop_Delete = GameObject.Find("Pop_Delete");
  Pop_Delete.SendMessage("PopDelStartAnimation");
 }


 // I want to delete the 3D object after clicking the "Delete Button" displayed on the screen above.
public void DelObj()
{
  // I'd like to put the value of the 3D object I clicked on in "***"...
  Destroy(***);
 }
}


2022-09-30 14:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.