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
1
Question by cernst77 · Sep 06, 2011 at 12:20 AM · c#nullreferenceexceptiongetcomponent

Still strugging with GetComponent and accessing other objects (C#)

I am still struggling with nulls when trying to access a script on one object from a script on a completely different one.

I had asked this before, but had to put this task down for a couple weeks - went to revisit it and still struggled with no luck for hours even with the previous help I am still missing something.

I did manage to clean up my code and concept though in the mean time - still won't work though.

ERROR=NullReferenceException: Object reference not set to an instance of an object Button.OnMouseUp () (at Assets/Standard Assets/Scripts/SimHub CSharp/Button.cs:31) UnityEngine.SendMouseEvents:DoSendMouseEvents()

Offending line: (Button.OnMouseUp())

        factoryLift1.GoToFloor(2); <-- something apparently null when we get here, why?
PUBLIC MACHINE factoryLift1 up top should hold a value - but it will not take, what is my error anyone? please?

 }


using UnityEngine; using System.Collections;

public class Button : MonoBehaviour {

 public bool pushed = false;
 public Machine factoryLift1;
       
 void Start() {
   
     GameObject tmp = GameObject.Find("Lift1");

     if (tmp != null)
     {
         Machine factoryLift1 = tmp.GetComponent<Machine>();
                     
     }
             

}

 void OnMouseEnter() {
     //renderer.material.SetColor("_OutlineColor", Color.blue); .//not important right now
 }

 void OnMouseExit() {
     //renderer.material.SetColor("_OutlineColor", Color.black); //not important right now
 }

 void OnMouseUp() {
     pushed = true;
     factoryLift1.GoToFloor(2);
           
 }

    
 void Done() {
     pushed = false;
 }
 

}

using UnityEngine; using System.Collections;

public class Machine : MonoBehaviour {

 public Transform target;
     
 private float floor1Height = 0F;
 private float floor2Height = 10F;
 
 private int floorNum;
 public Vector3 myDestination;
 private Vector3 velocity = Vector3.zero;
 public bool isActive = false;
 Vector3 floor1Destination; // start on ground
 Vector3 floor2Destination;
 
 // Use this for initialization
 void Start()
 {

     isActive = false;
     floor1Destination = target.transform.position; // original Vector3 Position - should be ground floor
     myDestination = floor1Destination; // set floor 1 = ground floor and starting position vector3

     Vector3 targetPosition = target.TransformPoint(new Vector3(0, 0, floor2Height)); // Calc new target vector3
     floor2Destination = targetPosition; // New Vector3 Position
 }
    
 void Update() {
     if(isActive)
     {
         MoveMe();
     }
 }

 void MoveMe()
 {
     // Move the elevator

     if (isActive == true)
     {
         float translation = Time.deltaTime * 2; // how smooth the motion is per update

         // Elevator Up
         target.Translate(Vector3.back * translation); //going up
         if (target.transform.position.y > myDestination.y)
              isActive = false; // Reached our floor
                   
     }
 }

 public void GoToFloor(int floorNum)
 {
     switch(floorNum)
     {
         case 1:
             myDestination = floor1Destination;
             break;
         case 2:
             myDestination = floor2Destination;
             break;
         default:
             myDestination = floor1Destination;
             break;
     }

     isActive = true;
 }

 void StopMe()
 {
     isActive = false;
 }

 void ResumeMe()
 {
     isActive = true;
 }

}

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

2 Replies

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

Answer by aldonaletto · Sep 06, 2011 at 02:34 AM

The problem is in the line Machine factoryLift1 = tmp.GetComponent... : it's creating a temporary factoryLift1 variable inside Start, so the public factoryLift1 never sees the reference to the script Machine.cs. Remove Machine from this line and it should solve your problem:

... public bool pushed = false; public Machine factoryLift1;

 void Start() {
     GameObject tmp = GameObject.Find("Lift1");
     if (tmp != null)
     { // remove this Machine before factoryLift1!
         Machine factoryLift1 = tmp.GetComponent&lt;Machine&gt;();    
     }

}

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
1

Answer by cernst77 · Sep 06, 2011 at 02:43 AM

Thanks aldonaletto!

This works, Where is the thing on the page to mark this Answered? (Newbie here)

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

Object reference not set to an instance of an object in c#. 0 Answers

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer

I'm having more problems accessing a variable from another script in c# 2 Answers

NullReferenceException when trying to access other GO in script. C# 1 Answer

Multiple Cars not working 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