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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TaylorAnderson · Jul 30, 2012 at 08:32 PM · platformerswitchnull reference exceptionmoving platform

"Object reference not set to an instance of an object" error

I have a switch and a platform that moves upward when the switch is triggered. I'm getting the error "NullReferenceException: Object reference not set to an instance of an object" when I try to use GetComponent to use the switch's script in the platform's code. When I looked up this error message on Unity Answers a lot of people said that it was because Unity couldn't find a component I needed, so I doublechecked that the switch's script was attached to the moving platform game object, and it is--however, whenever I press play to run my game, the script disappears. Does anyone know what's going on? The code for the moving platform is:

using UnityEngine; using System.Collections;

 public class MovingPlatform : MonoBehaviour 
 {
  public SwitchScript nearSwitch;
  void Awake ()
  {
  nearSwitch=gameObject.GetComponent<SwitchScript>();
  }
  
  void Update () 
  {
  if (nearSwitch.isSwitched)
  transform.Translate(Vector3.up);
  
  }
 }

I plan to add code to stop the platform moving up later, I just need the switch boolean to activate the moving platform.

The code for SwitchScript is

 using UnityEngine;
 using System.Collections;
 
 public class SwitchScript : MonoBehaviour 
 {
  public bool isSwitched=false;
 
  void OnTriggerEnter (Collider otherObject)
  {
  renderer.material.color=Color.green;
  isSwitched=true;
  
  }
 }

Comment
Add comment · Show 4
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 whydoidoit · Jul 30, 2012 at 08:34 PM 0
Share

You file is called movingplatform.cs (all in lower case), right?

avatar image whydoidoit · Jul 30, 2012 at 08:35 PM 0
Share

And switchscript is called switchscript.cs (again in lowercase?)

You should really call scripts using the na$$anonymous$$g convention of a capital first letter then camel case for the other words. It makes it a lot easier to read when formatted that way and will be in line with other people's code.

Also the line nearSwitch = would be much better and safer as:

     nearSwitch = gameObject.GetComponent<switchscript>();

And it would be better to cache it in Awake to give better performance.

avatar image Kryptos · Jul 30, 2012 at 08:46 PM 0
Share

Can you put the code for the switchscript (which should be written SwitchScript by now)?

avatar image TaylorAnderson · Jul 30, 2012 at 10:59 PM 0
Share

The na$$anonymous$$g style has changed, and the SwitchScript code has been posted.

2 Replies

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

Answer by TaylorAnderson · Aug 05, 2012 at 04:28 AM

This is the code I ended up using, just inside the switch script itself. I'll leave it here for reference in case any other newbies have the same issue.

 using UnityEngine;
 using System.Collections;
 
 public class ElevatorSwitchScript : MonoBehaviour 
 {
  //all same as doorswitch.
  public bool isSwitched=false;
  
  public float platformUp=5;
  
  public Transform elevator;
  
  public Vector3 desiredPos = new Vector3(0f, 0f, 0f);
 
  void OnTriggerEnter (Collider otherObject)
  {
  renderer.material.color=Color.green;
  isSwitched=true;
  }
  
  void Update()
  {
  if (isSwitched==true && elevator.transform.position.y <=  desiredPos.y)
  elevator.transform.Translate (Vector3.up * platformUp * Time.deltaTime);
  }
 }
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
avatar image
0

Answer by rhodnius · Aug 05, 2012 at 03:26 AM

Hi TaylorAnderson,

It seems that your problem is that you are trying to Get a component out of the gameObject that has the PlatformScript and not the gameObject that has the SwitchScript, you should try to create a public variable on your platform script which contains the gameObject that has the SwitchScript so then you could use the GetComponent on that object, try something like this:

 public class MovingPlatform : MonoBehaviour 
 {
  public GameObject switchObject; // <---- in the inspector drag&drop here the gameObject that has the SwitchScript
  public SwitchScript nearSwitch;
  void Awake ()
  {
  nearSwitch=switchObject.GetComponent<SwitchScript>(); //In here change gameObject by switchObject
  }
 
  void Update () 
  {
  if (nearSwitch.isSwitched)
  transform.Translate(Vector3.up);
 
  }
 }


Now it should work. Try it out and let us know.

Comment
Add comment · Show 1 · 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 TaylorAnderson · Aug 05, 2012 at 03:37 AM 0
Share

Shoot, sorry, I've managed to figure it out on my own. I just put all of the code to move the platform inside of the switch script. I'll post the way I did it in a second

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

9 People are following this question.

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

Related Questions

Platfomer 2d game block destroying raycasting nullreference exception problem 1 Answer

Character on a moving platform slows down 1 Answer

One-way Platforms (2D) 1 Answer

How To Change Player As Soon as He Take Powerup 1 Answer

2D 360 degress platformer example needed 0 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