Solved. Sort of.
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;
}
}
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.
Follow this Question
Related Questions
Unity Game stops responding when serial port is open? 0 Answers
Problem with Application.Quit 2 Answers
Creating dynamically moving buttons/GUI objects. 1 Answer
A doubt on loading Scenes 0 Answers