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 /
avatar image
0
Question by Malixx · Oct 04, 2016 at 05:14 AM · gameobjectreferencecall

Unity 5 Calling Function From Another Script On A Different Object

So I'm trying to get a function to run from a parent script when I click on an object with a particular tag. Script A (RayCastHit) is for my raycast and tests if it has hit the particular object while Script B(PickUpReactions) is for reducing a count. Script B is attached to the parent object while Script A is attached to the child.

using UnityEngine; using System.Collections;

public class RayCastHit : MonoBehaviour {

 public int PowerAmount = 1;
 public float fireRate = 0.25f;
 public float weaponRange = 50f;
 public Camera MainCam;
 public AudioClip ShotsFired;
 private float nextFire;

 void Start()
 {
     MainCam = GetComponentInParent<Camera>();
 }


 void Update()
 {
     if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
     {
         nextFire = Time.time + fireRate;
         Vector3 rayOrigin = MainCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
         RaycastHit hit;
     if (Physics.Raycast(rayOrigin, MainCam.transform.forward, out hit, weaponRange))
         {
             if (hit.transform.gameObject.tag == "Generator")
             {
                 gameObject.GetComponent<PickUpReactions>().ReduceCount();
                 AudioSource.PlayClipAtPoint(ShotsFired, transform.position);
             }
         }
     }
 }

}

but because of the "gameObject.GetComponent().ReduceCount();" line, it comes up with the error "NullReferenceException: Object reference not set to an instance of an object RayCastHit.Update () (at Assets/Scripts/RayCastHit.cs:41)". he other script looks like this.

using UnityEngine; using System; using UnityEngine.UI; using System.Collections;

public class PickUpReactions : MonoBehaviour { public int count;

 void Start () {
     count = 0;
 }

 public void ReduceCount()
 {
     if (count >= 0)
     {
         count = count - 1;
     }
 }

} I've spent hours googling solutions that don't work yet so I don't know why they don't want to talk.

Comment
Add comment · Show 3
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 JedBeryll · Oct 04, 2016 at 05:26 AM 0
Share

So PickUpReactions is attached to the parent of RayCastHit?

avatar image Malixx JedBeryll · Oct 04, 2016 at 06:00 AM 0
Share

Yes FPSController (PickUpReactions is here) | ---> Launcher (RayCastHit is here)

avatar image saschandroid · Oct 04, 2016 at 06:27 AM 0
Share
 gameObject.GetComponent<PickUpReactions>().ReduceCount();

The gameObject here is the game object with the RayCastHit script on it ("The game object this component is attached to.") If I understand your question correctly you want to use:

 hit.transform.gameObject.GetComponent<PickUpReactions>().ReduceCount();


1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lilius · Oct 04, 2016 at 06:22 AM

You are trying to find ReduceCount script on the same game object that has RayCastHit script.

Shouldn't this line: gameObject.GetComponent().ReduceCount();

be instead: transform.parent.gameObject.GetComponent().ReduceCount();

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you reference different cameras 2 Answers

gameObject are not referenced 2 Answers

How to get a referance to an instantiated prefab clone ? 1 Answer

Allocating Game Object name dynamically using Find 0 Answers

Object referencing - metafile changes on different computers 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