• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Frosh1 · Mar 06, 2020 at 03:30 PM · gameobjects

GAMEOBJECT AS ARGUMENT LOSS REFERENCE WHEN CALLING THE FUNCTION EITHER FROM ITS CLASS OR OUTSIDE CLASS.

Please help !

Here is my code

Script1

 Public class Test1: Monobehaviour{
 
 Public GameObject buttonprefab;
 Public Transform grid;
 Public delegate void Functionreturntype();
 
 
 
 public void SpawButton(GameObject clone, string buttonname, Functionreturntype)
 {
 Clone = (GameObject) Instantiate (buttonprefab,grid);
 Button button = clone.GetComponets();
 Butto .onClick.AddListener(delegate {Function type};});
 TextMeshProGUI buttontext = button.GetComponetInChildren();
 Buttontext.text = buttonname;
 
 }
 
 }

Scrip2

 Public class Test2: Monobehaviour{
 
 Public Test1 test1;
 Public GameObject myclone;
 
 Void Start   ( ){
 Test1.SpawButton(myclone,"opendoor", Opendoor);
 
 }


 Void Opendoor(){ 
 Debug.Log("opening") ;
  }
 
 
 }







Everything works fine and I get no error. The idea is to be able to delete clone button whenever I want to but the problem is that 'clone' from "script 1" never equal to clone of "script 2 " which is weird because I'm unable to delete the clone button whenever I call the function. I also tested it in inspector no luck.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Mar 06, 2020 at 03:39 PM 0
Share

Please format your code correctly...

avatar image Frosh1 · Mar 06, 2020 at 04:05 PM 0
Share

Sorry @ShadyProductions this my first time of posting in forums

avatar image Frosh1 · Mar 07, 2020 at 07:17 PM 0
Share

Pls anyhelp will be appreciated

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tormentoarmagedoom · Mar 07, 2020 at 08:39 PM

Hello.

You talk about destroy the clone, but i dont see any destroying code, only spawning.

I dont iunderstand your code..

You talk about so many difernt variables :

in script 1 you have :

 Clone
 clone

In script 2:

 myclone

why you call SpawButton() using myclone from script 2. But then in script 1 instantiate a prefab stored there and create a new variable called Clone ??

dont get what you want to do...


UPDATE


For your comments, then you just need to create a public variable in Script 1, and assign the new created clone in that variable, so from script 2 can reach thet object adn destroy it when you weant.

when you instantiate something, you can define it in a variable. This is the sctructure:

Script1:

 public GameObject NewObject;
 NewObject = Instantiate (prefab, pos, rot);

Script2:

 Destroy(Script1.NewObject);
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frosh1 · Mar 08, 2020 at 02:12 AM 0
Share

Thanks @tormentoarmagedoom what I'm trying to do is how to destroy new spawn clone in another script2?

avatar image tormentoarmagedoom Frosh1 · Mar 08, 2020 at 11:57 AM 0
Share

Updated answer :D

avatar image Frosh1 · Mar 08, 2020 at 12:34 PM 0
Share

You're life saver @tormentoarmagedoom I really appreciate

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can u "bind" game objects together to increase performence in an explosion or traffic jam? 1 Answer

Multiple Fire Points 2 Answers

Prebuilt gameobject bundles without Prefab instancing behavior. 0 Answers

How do I make it so every object the player creates (in a building game) gets a script attach to it by default? 4 Answers

Placing object with ray cast 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges