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 henmachuca · Oct 26, 2016 at 05:46 AM · script.objectreferencemethod

Can't access a method from another script (Object reference not set to an instance of an object)

Hello community!

I have a script Called Projectile where I have some code in order to be able to do object pooling in it and in my Character Controller Script I have set a quick test to try things out and see if the calling method works but I get the object reference not set and I can't figure my way out of it.

Can anyone help me out please?

 public class Projectile : MonoBehaviour
 {
     public Transform target;
 
     public float moveSpeed = 100f;          // speed at which the projtive moves towards the target
     public float rotationSpeed = 10f;      // speed at which the project rotates towards the target
     public float shootTime = 1f;
 
     public bool updateRotation = false;     // whether or not the projectile should continue to update its rotation after it was spawned
     public int pooledAmount = 3;         // max amount of arrows that will be able to be shoot at once
 
     public GameObject arrow;
     public List<GameObject> arrows;            // create a list containing all the arrows that will be allocated to memory once it starts
 
     void Start()
     {
         transform.LookAt(target);   // set the default direction of the projectile to face towards the target
         arrows = new List<GameObject>();
         for (int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(arrow);
             obj.SetActive(false);
             arrows.Add(obj);
         }
     }
 
     void Update()
     {
         Rotate();
         Move();
     }
 
     /// <summary>
     /// Rotates the projectile towards the target.
     /// </summary>
     void Rotate()
     {
         Vector3 targetPosition = target.position - transform.position;
 
         float speed = rotationSpeed * Time.deltaTime;
 
         Vector3 targetDirection = Vector3.RotateTowards(transform.forward, targetPosition, speed, 0);
 
         transform.rotation = Quaternion.LookRotation(targetDirection);
     }
 
     /// <summary>
     /// Move the projectile towards the target.
     /// </summary>
     void Move()
     {
         transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
     }
 
     /// <summary>
     /// "Destorys" the object
     /// </summary>
     void OnEnable ()
     {
         Invoke("Destroy", 2f);
     }
     void Destory ()
     {
         gameObject.SetActive(false);
     }
     void OnDisable ()
     {
         CancelInvoke();
     }
 
     public void Fire()
     {
         for (int i = 0; i < arrows.Count; i++)     // loops through the list
         {
             if (!arrows[i].activeInHierarchy)      // if the arrow is not currently active (looking for inactive arrows)
             {
                 arrows[i].transform.position = transform.position;
                 arrows[i].transform.rotation = Quaternion.identity;
                 arrows[i].SetActive(true);
                 break;
             }
         }
     }
 }


Script where I try to call the method FIRE from the previous one:

 public class Player_ClickToMove : MonoBehaviour
 {
     public Projectile projectileScript;
 
     public GameObject target;
 
     public GameObject arrowTest;
     public Transform arrowTargetTest;
 
     void Awake ()
     {
         projectileScript = GetComponent<Projectile>();  
     }
 
             if (Input.GetKeyDown(KeyCode.Z))
             {
                 projectileScript.GetComponent<Projectile>().Fire();
                 Debug.Log("Shooted");
             }
         }
     }


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 NoseKills · Oct 26, 2016 at 06:22 AM 0
Share

A) you should mention the line that throws the error ins$$anonymous$$d of making us read all 100 lines and guess. The line number should be mentioned in the error message.

B) There's no need to get the Projectile script from your Projectile script projectileScript.GetComponent<Projectile>().Fire();. What you do in Awake should be enough. You are already GetComponent()ing said component in there.

C) based on the na$$anonymous$$g I would guess your Player_ClickTo$$anonymous$$ove script is attached to the player object? If it is, you are trying to get a Projectile script attached to your player object. Is there a Projectile script attached to the same object as the Player_ClickTo$$anonymous$$ove script? If there isn't, projectileScript.GetComponent<Projectile>() will return null and calling Fire() on a null Unity.Object reference will cause a the error you are getting.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by henmachuca · Oct 27, 2016 at 04:35 PM

Indeed I managed to find my way around it with your tips.

Thank you @NoseKills, and sorry for not showing the code line with the error. I'll do that for my next questions.

Cheers.

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

[Help] how do I reference/access another script in unity C# 2 Answers

Creating an object using a prefab, with parent, text etc.. 1 Answer

Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer

IndexOutOfRangeException: Array index is out of range. 2 Answers

Help for Quest System 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