How do I access a gameobject from another class

Updated on June 2, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on June 2, 2018

I already did my research but theobject reference not set to an instance of an object error is still occuring. basically I made a button ‘okay’ where if you press it , its gonna disable the gameobjects that appeared once you get the AK. So I need to access the game object ‘alrightButton’ in the ak class but I tried my research but it still wont work

 

 

 

 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gettingAK : MonoBehaviour {
 public GameObject firstMission;
 public GameObject alrightButton;
 public GameObject getAK;
 public GameObject gotAK;
 public GameObject tableAK;
 private bool gotgotAK;
 private void OnTriggerStay(Collider other)
 {
 getAK.SetActive(true);
if (Input.GetKeyDown(KeyCode.E))
 {
 gotgotAK = true;
 gotAK.SetActive(true);
 Destroy(tableAK);
 }
 if(gotgotAK == true)
 {
 getAK.SetActive(false);
 firstMission.SetActive(true);
 alrightButton.SetActive(true);
 }
 }
 private void OnTriggerExit(Collider other)
 {
 getAK.SetActive(false);
if (gotgotAK == true)
 {
 gotAK.SetActive(true);
 }
 else
 {
 gotAK.SetActive(false);
 }
 }
 // Use this for initialization
}
} 
 
 
-------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class alrightyButton : MonoBehaviour {
private gettingAK getAKClass;
 void Start()
 {
 getAKClass = GameObject.Find("gunTrigger").GetComponent <gettingAK>();
 }
 public void OnClickmeh()
 {
 getAKClass.firstMission.SetActive(false);
 }
}
  • Liked by
Reply
0 on June 2, 2018

No errors but nothing is happening too

  • Liked by
Reply
Cancel
0 on June 2, 2018

The alrightButton class is attached to an empty object because this OnClickmeh is for my button when it was click

  • Liked by
Reply
Cancel