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
0
Question by Knightraven8 · Oct 09, 2012 at 07:21 AM · callanotherfrom

I'm having trouble referencing a function from one script inside another

I trying to set up a script that keeps track of the hit points for bricks that are hit by a cannonball. I set it up using 2 scripts, one for the bricks (the script is attached to a prefab of the bricks) that keeps track of a hit point variable, and a second for the cannonball (attached to the prefab of the cannonball being instantiated). The cannonball script is supposed to call the brick script and update the HP if it detects collision with a brick.

Right now, it seems to be able to detect collision just fine, but I'm having difficulty getting the 1st script to call the second. I keep getting a null reference error saying that the object reference is not set to an instance of the object, an I'm not sure why. I dragged the game objects into the correct place in the inspector for both scripts. Am I doing something wrong?

This is my BrickHP.js:

 var normalBrick : GameObject;
 var crackedTexture: UnityEngine.Texture;
 
 var hitPoints : float = 100; 
 
 function Start(){
 
 }
 
 function checkHP() {
     if (hitPoints <= 50){
         renderer.material.mainTexture = crackedTexture;
     }
     if (hitPoints <= 0) {
         Destroy(normalBrick);
     }
     Debug.Log("I hit it");
 }

And this is my cannonball script:

 var normalBrick : GameObject;
 var cannonball: GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     
 }
 
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.name == "Small Brick"){
         Debug.Log(collision.gameObject.name);
         var brickHP: BrickHP = GetComponent(BrickHP); 
         brickHP.checkHP();
     } 
 }

I'll finish the rest of the code for calculating the actual HP later. Right now I'm just trying to figure out if I'm calling the script correcly.

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 Fattie · Oct 09, 2012 at 07:52 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/

see the two "Accessing" links on the left hand side ...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Oct 09, 2012 at 07:22 AM

You need to call the GetComponent on the thing you hit I'd guess...

   var brickHP = collision.gameObject.GetComponent(BrickHP);
Comment
Add comment · Show 6 · 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 Knightraven8 · Oct 09, 2012 at 07:50 AM 0
Share

I tried to do this and Unity would not build the code

avatar image whydoidoit · Oct 09, 2012 at 07:52 AM 0
Share

With what error? That's the right way to do it.

avatar image Knightraven8 · Oct 09, 2012 at 09:05 AM 0
Share

It isn't compiling correctly. This is what I put, but it gives me a message saying it is expecting a ;

 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.name == "Small Brick"){
         Debug.Log(collision.gameObject.name);
         var brickHP: collision.gameObject.GetComponent(BrickHP); 
         brickHP.checkHP();
    }      
 }
avatar image whydoidoit · Oct 09, 2012 at 09:21 AM 0
Share

It should be an = not a : between the definition and the assignment.

avatar image Knightraven8 · Oct 09, 2012 at 09:37 AM 0
Share

Haha well I guess it helps if your syntax is correct! That fixed it, thanks!

Show more comments

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

11 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

Related Questions

Call a Function from another Script 1 Answer

Can't access variable from another script. 1 Answer

Call inspector editor script function from another editor script 2 Answers

Call function in another script? 1 Answer

C# - How do I increase the variable from another script? 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