Unable to use Functions from another script [C#] READ THRU MANY Q&As but STILL not working
HELP! Even though I read through many many Q&As of similar nature, I still can't solve my own problem. According to my debug log, only "YOU DEAD!" popped up and not "cuteCube is DEAD!" which means my Player.Control function which I call from another script - cuteCube is not working.
public class lightningKills : MonoBehaviour {
private Collision2D c;
public float howHeavy;
public bool isDestroyed;
void Start()
{
howHeavy = GetComponent<Rigidbody2D>().mass;
}
void OnCollisionEnter2D(Collision2D c)
{
switch(c.gameObject.tag)
{
case "Player":
Debug.Log("YOU DEAD!");
cuteCube PlayerControl = gameObject.GetComponent<cuteCube>();
//Kills the Player!
PlayerControl.Death();
Destroy(gameObject);
isDestroyed = true;
Debug.Log("cubeCube is DEAD!");
break;
case "Background":
if (gameObject.name == "lightning(Clone)")
{
Destroy(gameObject);
isDestroyed = true;
Debug.Log("Destroyed!");
}
break;
default:
Destroy(gameObject);
if (gameObject.name == "lightning(Clone)")
{
Destroy(gameObject);
isDestroyed = true;
Debug.Log("Destroyed!");
}
break;
}
}
}
This is the script that I want to reference to (focus on the Death() function):
using UnityEngine;
using System.Collections;
using System;
public class cuteCube : MonoBehaviour
{
private bool facingRight = true;
private Transform groundCheck;
private Animator anim;
public Collision2D col;
private float straight;
private ConstantForce2D pushIt;
public BoxCollider2D fallThrough;
public float dyingJump = 200f;
private Rigidbody2D box;
public GameObject restartGameUI;
private bool isShowing;
void Start()
{
// Setting up references.
groundCheck = transform.Find("groundCheck");
isShowing = false;
anim = GetComponent<Animator>();
fallThrough.isTrigger = false;
move();
restartMenu();
}
void FixedUpdate()
{
checkClickInput();
// checkTouchInput();
}
void Flip()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
Debug.Log("HITTING THE WALL, I'M GOING TO FLIP NOW!");
//Make the cuteCube go in opposite direction.
pushIt.force = -pushIt.force;
move();
}
void move()
{
//Giving the cuteCube a constant force for it to automatically move.
pushIt = GetComponent<ConstantForce2D>();
}
/* void checkTouchInput()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if (hit != null)
{
Flip();
Debug.Log("Touched it");
}
}
}
*/
void checkClickInput()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (Input.GetMouseButtonDown(0))
{
if (hit != null)
{
Flip();
Debug.Log("I'm hitting " + hit.collider.name);
}
}
}
//Detecting collision with the wall.
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Walls" )
{
Flip();
}
}
public void Death()
{
pushIt.enabled = false;
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, dyingJump));
fallThrough.isTrigger = true;
Dead();
}
void restartMenu()
{
restartGameUI.SetActive(isShowing);
}
void Dead()
{
isShowing = true;
restartMenu();
}
}
Answer by GodFather2747 · Dec 14, 2015 at 10:16 AM
Hi you can use SharedInstance in cuteCube to access this class function from other class
private static cuteCube _instance = null;
public static cuteCube SharedInstance
{
get
{
// if the instance hasn't been assigned then search for it
if (_instance == null)
{
_instance = GameObject.FindObjectOfType (typeof(cuteCube)) as cuteCube;
}
return _instance;
}
}
then call function death from lightningKills script like
cuteCube. SharedInstance.Death();
Thank you very much! It worked! Even though I don't understand how it works... if you don't $$anonymous$$d can explain to me?
Your answer
Follow this Question
Related Questions
Problem with force movement when calling from another scripts 0 Answers
How to change/disable/enable things like textures/hitboxes etc. in game 0 Answers
Why is a string reference different to a string? 1 Answer
Unity and external plugins 0 Answers
An object reference is required to access non-static member/ Referencing Function 1 Answer