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 UnverifiedBacon · Dec 22, 2015 at 12:58 PM · c#scripting beginnerfunction call

Unable to call a function from another script

Hi,

I'm have a ton of trouble successfully calling this function. I believe I have it set up right, and my code matches other 'solutions' I've found already. Everything compiles, but Unity doesn't call the function, and gives me this error: alt text

I have made sure that the CameraShake script is attached to the MainCamera in the scene (only camera in the scene.)

This is my code: alt text

As you can see, the "test shake" prints, but the error happens when it tries to call that next line. Also, I tried declaring it like this, which had the same outcome: CameraShake shake = this.gameObject.GetComponent();

Any insight would be greatly appreciated.

3.png (8.5 kB)
4.png (67.4 kB)
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 UnverifiedBacon · Dec 22, 2015 at 01:11 PM 0
Share

Upon further inspection, I see that the issue is becasue the CameraShake script was not attached to the bricks -- the objects which have the sfript that is calling the CameraShake cript. the problem is now that I'm not able to attach the scripts to either the prefab, or the instances of the bricks. I get the "Ghost Buster/No Smoking" symbol when I try.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Dec 22, 2015 at 01:06 PM

Hi

You don't set the 'shake' variable anywhere.

As a result, it is 'null' (i.e. not set), and you are getting a 'null reference exception' - this is the scripts way of telling you that you are are trying to reference an object that is null.

Assuming CameraShake is a component of the same object as Brick, you could add the line:

 shake = gameObject.GetComponent<CameraShake>();

to the Start function.

Note that this must be done int he Start function - not as part of the variable definition. If you place it in the variable definition, it is executed when your component is first created in memory, and there is no guarantee anything else has been created at that time!

If this doesn't work, then your brick object probably doesn't have a camera shake component attached.

p.s. in future always post code using the code formatting option in the help forums - see the little 101010 icon in the text editor!

-Chris

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 UnverifiedBacon · Dec 22, 2015 at 01:17 PM 0
Share

Thanks for responding!

I now have the code:

public class Brick : $$anonymous$$onoBehaviour {

 public Sprite[] hitSprites;
 public static int breakableCount = 0;
 public AudioClip crack;
 public GameObject smoke;
 //public CameraShake shake;
 public CameraShake shake; 
 private int timesHit;
 private Level$$anonymous$$anager level$$anonymous$$anager;
 private bool isBreakable;


 void Start () {
     shake = gameObject.GetComponent<CameraShake>();
     isBreakable = (this.tag == "Breakable");
     if (isBreakable) {
         breakableCount++;

     }
     timesHit = 0;
     level$$anonymous$$anager = GameObject.FindObjectOfType<Level$$anonymous$$anager> ();

 }





It still gives the error, and I see that the brick gameObject is still asking for something to be added to it, but will not allow me to.

avatar image
0

Answer by UnverifiedBacon · Dec 22, 2015 at 01:41 PM

I found that if I manually add a NewScript > CameraShake, the code works. I deleted "public" in front of CameraShake shake; when I declared it, so that the brick was no longer asking for it to be attached. In any case, I have my answer now.

Thank you!

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

44 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

Related Questions

How do you continuously rotate an object? 1 Answer

Script not working for me 2 Answers

[Solved] I am trying to build a space sim/ shooter type game. 1 Answer

Accessing string from other script 1 Answer

Question about triggering an event (Extremely Novice to Programming) 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