Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by shererd · May 26, 2016 at 01:02 PM · gameobjects

how to call a function with a gameobject as a argument

I am currently having problems using a gameobject as an argument in a function.

Here is how I am trying to call the held function from another script.


Backpack.Held(0, gameObject);


below is the function in the Backpack script:


public void Held(int a, GameObject b) { }


I have tried setting the game object into a variable which did not work. I don't have to use this method to pass the game object.

for reference my situation is as follows: I pick up one of many object which has the same script, when picked up I must let another(lets say it is the main) script know which gameobject has been picked up.


I suppose I could create an empty gameobject in the main script and set it as the 'pick up's game object in the pick up script. however I would like to know if I could achieve this using my first method,(and if the second method would work)

all help is appreciated thanks

Comment
Add comment · Show 4
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 Hellium · May 26, 2016 at 10:17 AM 0
Share

Have you tried passing the gameobject by reference ?

https://msdn.microsoft.com/en-us/library/s6938f28.aspx

avatar image Dave-Carlile · May 26, 2016 at 01:08 PM 1
Share

Yes, you can pass a game object as a parameter, and your sample code looks like the correcct way for doing that. What exact problems are you having? What "didn't work"?

avatar image shererd · May 26, 2016 at 06:35 PM 0
Share

it did not compile, the message i received was as folows: error CS0120: An object reference is required to access non-static member.

this seems related to what @Hellium has said however i iam unsure of how to change it

avatar image TreyH · May 26, 2016 at 07:08 PM 1
Share

Going by the error you mentioned in the comments, you can't call it like that. Unless you have an instance of the BackPack class, you can't call a member function. It should work if you change that method to public static void, but this may change your intended functionality.

1 Reply

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

Answer by Hellium · May 27, 2016 at 07:13 AM

@TreyH seems to be right, I haven't seen the way you call the function.

You need an instance of the Backpack class to call the Held function since it's not a static method of the class.

If the Backpack class inherits from MonoBehaviour, you must get the component / find the object holding the script :

 GetComponent<BackPack>().Held(  0, yourGameObject ) ; // If the component is on the same object as your current script
  FindObjectOfType<BackPack>().Held(  0, yourGameObject ) ; // If the component is on an other gameobject

If the backpack does not inherit from MonoBehaviour :

 Backpack pack = new Backpack();
  pack.Held( 0, yourGameObject);
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

checking if gameobject exists 1 Answer

How to Serialize or Save a List of GameObjects 1 Answer

Spawn objects in a row with same spacing 0 Answers

How to change the shape of gameObjects 2 Answers

Accessing values on a gameobject 1 Answer


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