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 /
This question was closed Mar 04, 2014 at 02:43 PM by Dblfstr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dblfstr · Feb 04, 2014 at 08:43 PM · c#bool

NullReferenceException: When accessing bools from another game object's script.

So I have this code below. What it does is simulate the floor moving by offsetting the texture material at a certain pace. That is not important, however, as my problem lies within line 16. I have a bool "dead" as a variable in my Platformer2DUserControl.cs. It is attached to a fish. Everything moves with the fish as he goes through the level (that is why I have the texture offset, so it looks like the floor is stationary). I want this floor scrolling to stop when the fish dies because the fish stops when he dies.

Everything actually works out as planned. As the fish moves, the floor texture scrolls backwards and it looks like the fish is moving, when he dies the floor stops, and looks like I am not moving. But I still get this error.

EDIT: Okay, the floor is not actually scrolling. So nothing is happening after if(!Fish.dead) Is this not how you access a bool variable from another script?

 using UnityEngine;
 using System.Collections;
 
 public class floorscroller : MonoBehaviour {
 
     public float speed;
 
     Platformer2DUserControl Fish;
 
     void Start(){
     
         Fish=GetComponent<Platformer2DUserControl>();
 
     }
 
 
     void Update () 
     {
         if(!Fish.dead){ //This is the line with the error (Line 16)
             renderer.material.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
         }
         else
         {
             renderer.material.mainTextureOffset = new Vector2(0f, 0f);
         }
     }
 }

Comment
Add comment · Show 5
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 robertbu · Feb 04, 2014 at 08:45 PM 0
Share

Are you destroying the fish when he dies? If so your 'Fish.dead' reference is bad.

avatar image Dblfstr · Feb 04, 2014 at 08:55 PM 0
Share

I thought of that, and the collider script (the only thing that would kill the fish) just changes the the Fish' swim animation to dead. No where do I destroy the object. In fact, he just turns blue and goes belly up, then floats to the top of the screen. (I will have to double check to make sure nothing is getting destroyed though)

avatar image Dblfstr · Feb 04, 2014 at 08:58 PM 0
Share

The odd part is everything works fine. The texture stops scrolling, the fish enters his dead animation. The camera is set to follow the fish, and most objects are attached to the camera, so everything stops when the fish collides, but he is still there, just in his dead animation.

avatar image Dblfstr · Feb 04, 2014 at 11:00 PM 0
Share

Okay, the floor is not actually scrolling. So nothing is happening after if(!Fish.dead) Is this not how you access a bool variable from another script?

avatar image Dblfstr · Feb 05, 2014 at 02:21 PM 0
Share

I got it to work, as you can see in my answer. however, I do not know why it happened, or why what I did eventually worked. Could it be because I was assigning the variable Fish as a Platformer2DUserControl (referencing the script of that name). But the actual variable I was using was a bool?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Dblfstr · Feb 04, 2014 at 11:19 PM

// Figured it out, should have just called the script like so.

  using UnityEngine;
  using System.Collections;
     
     public class floorscroller : MonoBehaviour {
     
         public float speed;
         
         void Update () 
         {
             if(!Platformer2DUserControl.dead){ //This is the line with the error (Line 16)
                 renderer.material.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
             }
             else
             {
                 renderer.material.mainTextureOffset = new Vector2(0f, 0f);
             }
         }
     }
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

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a light turn on and off with the same button 1 Answer

How do I stop jumping in midair? 4 Answers

how to use bool from another script in if statment? 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