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 heyhujiao · Dec 14, 2015 at 09:35 AM · c#referencefunctions

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();
     }
 }
 


Comment
Add comment
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

1 Reply

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

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();

Comment
Add comment · Show 1 · 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
avatar image heyhujiao · Dec 14, 2015 at 02:50 PM 0
Share

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

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

37 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

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


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