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
2
Question by PanayiotisPetsas · Jun 26, 2018 at 06:06 AM · gameobjectgetcomponentboolean

How can I access a GameObject's script from a prefab?

Hello everyone,

I have recently posted this question on the forums with sadly no solutions :

https://forum.unity.com/threads/error-object-reference-not-set-to-an-instance-of-an-object-something-to-do-with-getcomponent.537336/

As you can see, I'm trying to access a another script from my prefab but I am not able to link the public script to my prefab. (It wont let me drag and drop it)

I basically want, depending if my character is looking up, down, left or right, for him to shoot in front of him (ie if he is looking up, he will shoot up)

I haven't been able to make the getcomponent work and I keep getting the following error: NullReferenceException: Object reference not set to an instance of an object BeamPrefabShooting.FixedUpdate () (at Assets/BeamPrefabShooting.cs:17) or sometimes it's 18

I'm really new to unity and coding, so please could someone help me? Thank you!!

for reference, here are the scripts:

  1. Player movement script:

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class PlayerMovement : MonoBehaviour { public float moreSpeed; private float speed = 0.02f; public Animator anim;

      public bool upAnim;
         public bool downAnim;
         public bool leftAnim;
         public bool rightAnim;
     
         public void MoreMovement()
         {
             //                          --------------- Extra Speed Movements ----------------
     
     
             if (Input.GetKey(KeyCode.UpArrow) && (Input.GetKey(KeyCode.Space)))
             { 
                 transform.Translate(0,speed + moreSpeed, 0);
             }
             if (Input.GetKey(KeyCode.DownArrow) && (Input.GetKey(KeyCode.Space)))
             {
                 transform.Translate(0,-speed - moreSpeed, 0);
             }
             if (Input.GetKey(KeyCode.RightArrow) && (Input.GetKey(KeyCode.Space)))
             {
                 transform.Translate(speed + moreSpeed, 0, 0);
             }
             if (Input.GetKey(KeyCode.LeftArrow) && (Input.GetKey(KeyCode.Space)))
             {
                 transform.Translate(-speed - moreSpeed, 0, 0);
             }
         }
         public void Movement() {
             //                        ---------------Normal Movements---------------
             if (Input.GetKey(KeyCode.UpArrow))
             {
                 //  xUp = xUp + speed;
                 transform.Translate(0, speed, 0);
                 anim.SetBool("Up", true);
                 anim.SetBool("Down", false);
                 anim.SetBool("Left", false);
                 anim.SetBool("Right", false);
                 upAnim = true;
                 downAnim = false;
                 leftAnim = false;
                 rightAnim = false;
             }
             if (Input.GetKey(KeyCode.DownArrow))
             {
                 //  xDown = xDown - speed;
                 transform.Translate(0, -speed, 0);
                 anim.SetBool("Up", false);
                 anim.SetBool("Down", true);
                 anim.SetBool("Left", false);
                 anim.SetBool("Right", false);
                 upAnim = false;
                 downAnim = true;
                 leftAnim = false;
                 rightAnim = false;
             }
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 //  yUp = yUp + speed;
                 transform.Translate(speed, 0, 0);
                 anim.SetBool("Up", false);
                 anim.SetBool("Down", false);
                 anim.SetBool("Left", false);
                 anim.SetBool("Right", true);
                 upAnim = false;
                 downAnim = false;
                 leftAnim = false;
                 rightAnim = true;
             }
             if (Input.GetKey(KeyCode.LeftArrow))
             {
                 //  yDown = yDown - speed;
                 transform.Translate(-speed, 0, 0);
                 anim.SetBool("Up", false);
                 anim.SetBool("Down", false);
                 anim.SetBool("Left", true);
                 anim.SetBool("Right", false);
                 upAnim = false;
                 downAnim = false;
                 leftAnim = true;
                 rightAnim = false;
             }
         }
     
         void Start () {
             anim = GetComponent<Animator>();
         }
     
         private void FixedUpdate()
         {
             //we always use FixedUpdate for physics (rigidbody2d)
             {
     
                 Movement();
                 MoreMovement();
               
             }
         }
         // Update is called once per frame
         void Update () {
     
         }
     }
     
    
    
  2. Prefab Script: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class BeamPrefabShooting : MonoBehaviour {

 public Rigidbody2D rb;
 public float bulletSpeed;
 public GameObject collidedBullet;
 public PlayerMovement playerMovement; // format:   private - Onoma tou script  - onoma tou gameobject pou epirieazete
                                       //                                         apo to script
  
 private void Start()
 {
     playerMovement = GetComponent<PlayerMovement>();
 }
 void FixedUpdate () {
     if (playerMovement.rightAnim == true)
     {
         rb.velocity = new Vector2(bulletSpeed, 0);
     }
     if (playerMovement.leftAnim == true)
     {
         rb.velocity = new Vector2(-bulletSpeed, 0);
     }
     if (playerMovement.upAnim == true)
     {
         rb.velocity = new Vector2(0, bulletSpeed);
     }
     if (playerMovement.downAnim == true)
     {
         rb.velocity = new Vector2(0, -bulletSpeed);
     }
     
 }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "wall")
     {
         Destroy(collidedBullet);
     }
 }
 private void Update()
 {

 }

}

  1. Pick Up Script:

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class BeamPickUp : MonoBehaviour { public Object Player; public Object pickUpBeam; public int itemCount; // Use this for initialization void Start () {

      }
         
         // Update is called once per frame
         void Update () {
             afterDestroyed();
     
         }
         void OnCollisionEnter2D(Collision2D Player)
         {
             if (Player.gameObject.tag == "Beam" )
             {
                 Destroy(pickUpBeam);
                 itemCount++;
                 print("Item count = " + itemCount);
             }
         }
     
         void afterDestroyed()
         {
          /*   if (pickUpBeam = pickUpBeam)
             {
                 print("not destroyed");
             }
             else
             {
                 print("destroyed");
             }
     
         */}  
     }
     
    
    
    
    

The whole idea is that if the player touches the item,the item gets destroyed and he then unlocks the ability to shoot beams.

Thank you all!!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by seandolan · Jun 26, 2018 at 06:17 AM

If you have 2 gameobjects each with a script attached to them, you can call the script and any public functions/variables from each other.

Let's call the objects Object1 and Object2 and the scripts Script1 and Script2 to make things easier to understand.

 // Script1's code attached to Object1
 public GameObject object2;
     
     void Start() {
        object2.GetComponent<Script2>.LogMessage("hello world");
     }
     
     // Script2's code attached to Object2
     
     public void LogMessage(string _message){
        Debug.Log(_message);
     }

This is how you can easily call a script function from another object.

Comment
Add comment · Show 8 · 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 seandolan · Jun 26, 2018 at 06:19 AM 0
Share

In this instance, you will need to drag Object2 into the public GameObject called "object2" attached to Object1

avatar image PanayiotisPetsas · Jun 26, 2018 at 09:25 AM 0
Share

Thank you so much for your answer. This makes things a lot easier thanks to the simplicity. I made everything as you said but there is one problem. I cannot assign components to a prefab from the actual gameObjects in the hierarchy. (I cannot drag and drop the 'player' object to the prefab's script where I should, it wont allow me)

Once again, thank you for your help.

avatar image seandolan PanayiotisPetsas · Jun 26, 2018 at 09:32 AM 0
Share

That's great you have gotten further. Does your player have a "Player" tag or assigned to a specific layer?

avatar image seandolan PanayiotisPetsas · Jun 26, 2018 at 09:37 AM 0
Share

If you assign the Player tag to your player object (that has a script attached) you could try something like this on the prefab's script:

 GameObject player;

Then in the Start() fuction add this:

 player = GameObject.FindWithTag("Player");

When you need to call the script do this:

 player.GetComponent<ScriptNameHere>().FireBullet(100);

This example assumes you have a public void FireBullet(float bulletSpeed) - but you can just do:

 player.GetComponent<ScriptNameHere>().FireBullet(); 

if you don't want to send it a value at the same time. You can also change variable values like this if you want:

 player.GetComponent<ScriptNameHere>().playerName = "PanaPetsas";



avatar image seandolan seandolan · Jun 26, 2018 at 09:39 AM 0
Share

The "GameObject player;" replaces "public GameObject object2;" in our original script

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

133 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 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 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 avatar image

Related Questions

GetComponent vs AddComponent 3 Answers

Unity crashes after I destroy a gameobject 1 Answer

C# GetComponent Issue 2 Answers

Acessing a Script Instance from Another Object 3 Answers

Can someone translate C# to Javascript? 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