Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by fritzlloydw · Nov 25, 2021 at 11:36 AM · c#componentreferenceself

Component passing self to method

I have a component called ItemSpot which is a container for an object of type Pickupable. When the player clicks on the ItemSpot, the item in the container is supposed to move from the spot to the player and the items reference is supposed to be "moved" from the spot to the PlayerController.

This is my code:

 public void OnClick(PlayerController player)
     {
         // Pick up items
         if (player.HeldItem == null && Item != null)
         {
 
             
             // Attach item to own transform 
             Item.transform.parent = player.HandTransform;
 
             // disable input to prevent glitches while taking an item in hand
             player.InputDisabled = true;
 
             // Coroutines on the Item that moves the gameObject with an animation
             StartCoroutine(Item.MoveTowards(player.HandTransform.position));
             StartCoroutine(Item.TurnTowards(player.HandTransform.rotation));
 
             Debug.Log(player.gameObject);
 
             // Move Item reference from Spot to player
             player.HeldItem = Item;
             Item = null;
             
             // reenable input after some time
             IEnumerator coroutine () {
                 yield return new WaitForSeconds(player.HeldItem.animationTime);
                 player.InputDisabled = false;
             }
             StartCoroutine(coroutine());
 
         }
 }

This bit is called from the PlayerController with itemSpot.OnClick(this)

Moving the GameObject works absolutely fine, but the references to the item do not change at all. Weirdly, Debug.Log(player.HeldItem) inside this function returns the correct reference, but in the PlayerController Component it is still null.

I suspect that this has something to do with m usage of the this operator, but I can't figure out how to properly pass a reference to self.

Comment
Add comment · Show 1
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 fritzlloydw · Nov 25, 2021 at 12:20 PM 0
Share

The most baffling thing to me is, that the ItemSpot is not able to set its own private field to null

1 Reply

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

Answer by fritzlloydw · Nov 25, 2021 at 12:30 PM

I found the solution and the issue is, of course, my own stupidity.

Just below the code I posted, I have the behaviour for passing back an item from the Player to the Itemspot. In it, I check, whether the Player has an Item in hand and the Itemspot is empty. Two conditions that will always be true, when the prior has run.

Therefore I am taking the item out of the ItemSpot and putting it right back in.

I'll let this stand to remind myself ad anyone who might come across it, to always check (and post) the full code

Thank you and good night

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

131 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Losing object reference 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Reference creator of gameObject 1 Answer

Anyway to pass by reference in a IEnumerator or something? 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