• 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 /
This question was closed Nov 08, 2016 at 01:00 PM by Thipi for the following reason:

Solved. Sort of.

avatar image
0
Question by Thipi · Nov 07, 2016 at 07:53 AM · buttongameobjects

Accessing gameObject through playerscript with button attached to the playerscript C#

Hey! I am rather new to Unity and I've been creating this silly CatGame that is meant to be played by mobiledevices. There are 2 buttons in the actual game: Jump (that works perfectly) and Drop(and this is the problem. So I have the function for the Drop in my playerscript where from I call the Drop function in the Button. The problem is that the function calls another function that is attached to a gameObject that I want to interact with. For some reason I don't even get a Debug when the button is pressed...?

So simple: I want the cat to drop the item off the table by either using isKinematic = false; or Destroy. :) Been working with this for some time now so any help is most welcome!

In the playerscript the function called by the button:

     public void DropButton()
     {
         GoodObject DropItem = gameObject.GetComponent<GoodObject>();
 
         if (gameObject.layer == LayerMask.NameToLayer("Item"))
         {
 
             DropItem.Destroy();
             Debug.Log("buttonDown!");
 
         }
     }

And the script for the Item:

 using UnityEngine;
 using System.Collections;
 
 public class GoodObject : MonoBehaviour {
 
     public int pointsToAdd;
     public Rigidbody2D itemRB;
 
     void Start() {
         Rigidbody2D itemRB = GetComponent<Rigidbody2D>();
         itemRB.isKinematic = true;
         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Item"), LayerMask.NameToLayer("Table"));
     }
 
 
 //Function called by the playerscript on buttonClick
     public void Destroy()
     {
         ScoreManager.AddPoints(pointsToAdd);
         itemRB.isKinematic = false;
         
     }
 }

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 Thipi · Nov 08, 2016 at 12:57 PM 0
Share

Never $$anonymous$$d! I solved it by discarding the entire Drop-button But I also did come up with alternative solution how the "Drop"-button could have worked: By instatiating a projectile(destroyed after 0.5 seconds and no velocity). That could've acted like "paw" and destroy the item.

0 Replies

  • Sort: 

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

81 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

Related Questions

Unity Game stops responding when serial port is open? 0 Answers

Functions on GUI elements disapearing after reloading the scene 1 Answer

Unity Button Script 0 Answers

getcomponent cant find every button script in every gameobject 1 Answer

Help with buttons 0 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