Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by forkmor · Oct 01, 2014 at 09:17 PM · functioncall

Calling a from another script c#

I want to run a function from another script. the scripts are on separate objects dunno if that matters

public class Chop_wood : MonoBehaviour { public int inventory = 0; void OnTriggerEnter (Collider col) { Debug.Log ("at the tree"); //Chop (); } public void Chop() { inventory++; Debug.Log ("i am chopping the wood"); } void Update () { } } And here is the other script, im trying to run the Chop() function but it tells me Object reference not set to an instance of an object chopped.OnTriggerEnter (UnityEngine.Collider col) (at Assets/chopped.cs:20)

 public class chopped : MonoBehaviour {
     public Chop_wood chopWood;
 
     void Start()
     {
         chopWood = GetComponent<Chop_wood> ();
     }
 
 
     void OnTriggerEnter (Collider col)
     {
 
         if (col.collider.name == "Pirate_Chopper") 
         {
 
             Debug.Log ("in the trigger");
             chopWood.Chop();
         }
     }
     
     void OnTriggerStay (Collider other)
     {
             
     }
 
 }
 

the funny thing is it worked once and that time it still gave me the error, whats wrong?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by giano574 · Oct 01, 2014 at 09:38 PM

It doesn't work because you are trying to get the Chop_wood component from the same GameObject as your second script is attached to.

 GetComponent<Chop_wood>();

is the same as

 this.GetComponent<Chop_wood>();

I assume the Chop_wood script is attached to another GameObject. The easiest way then, is to assign the GameObject with the Chop_wood script to the chopWood variable from the editor. You will notice that when selecting the GameObject with the chopped script in the hierarchy view that the chopWood variable is visible in the inspector. You can then drag the GameObject with the Chop_wood script to this variable. The editor will automatically take the correct script from the GameObject.

Then delete this line in the Start method:

 chopWood = GetComponent<Chop_wood>();
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
avatar image
1

Answer by psycocrusher · Oct 01, 2014 at 11:51 PM

Add the name of your gameobject in the parenthesis.The one that has the ChopWood function.

 void Start(){
 
     chopWood = GameObject.Find("").GetComponent<Chop_wood>();
     }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Calling a function from a different script on the same object 1 Answer

How to use UnityAdsVideoCompleted(); 0 Answers

Is there any way to check which method called a certain other method? 1 Answer

Need help with If/else statement. 1 Answer

How to reference another script and call a function in C# ? 2 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