x


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();
                        
        }
                
   }

    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;
    }
}
more ▼

asked Sep 06 '11 at 12:20 AM

cernst77 gravatar image

cernst77
-4 2 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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<Machine>();    
        }
   }
more ▼

answered Sep 06 '11 at 02:34 AM

aldonaletto gravatar image

aldonaletto
41.1k 16 42 195

(comments are locked)
10|3000 characters needed characters left

Thanks aldonaletto!

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

more ▼

answered Sep 06 '11 at 02:43 AM

cernst77 gravatar image

cernst77
-4 2 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4135
x404
x394

asked: Sep 06 '11 at 12:20 AM

Seen: 947 times

Last Updated: Sep 06 '11 at 02:43 AM