Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

(C#)Accessing a GameObject from a class, inside another class. On collision with other object.

$
0
0
Hello whoever might be reading this. First off, i'd like to apologize for any bad grammar that might occur. English is not my main Language. So, my problem is so : I got a script(lets call it scriptA), where i got a class inside of it(lets call it classA), that is attached to a gameobject. and i got another script, at another gameobject(lets call this scriptB), and i am trying to assign a variable from script scriptA's classA to a variable in scriptB, on collision. now, ive tried dozen of things, without luck. and the most common error it gives me is "NullReferenceException: Object reference not set to an instance of an object Equip.OnCollisionEnter (UnityEngine.Collision col) (at Assets/Player/SCRIPTS/Equip.cs:42)" Any help would be insanely appreaciated. **scriptA :** using UnityEngine; using System.Collections; public class BEGINNERSWORD : MonoBehaviour { static GameObject MODEL = Resources.Load("SWORD_MODEL", typeof(GameObject)) as GameObject; public class BEGINNERSWORD_01 : WEAPONS { void Init () { _NAME = "Beginner Sword"; _TYPE = 1; _VALUE = 001; _MODEL = MODEL; _ATTDMG = 10; } } //public BEGINNERSWORD_01 BS_01 = new BEGINNERSWORD_01; } **scriptB:** using UnityEngine; using System.Collections; public class Equip : MonoBehaviour { public GameObject Weapon = null; public Transform rHand; bool equiped = false; GameObject t = null; // Use this for initialization void Start () { } void equip(){ if(!equiped){ t = Instantiate (Weapon, rHand.position, rHand.rotation)as GameObject; t.transform.parent = rHand.transform; equiped = true; } else if (equiped){ Destroy((GameObject)t); equiped = false; } } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.E)){ if(Weapon = null){ Debug.Log("NoWeapon" + Weapon.tag); } else{ equip (); } } } public void OnCollisionEnter(Collision col){ if(col.gameObject.tag == "SWORD"){ BEGINNERSWORD bs = col.gameObject.GetComponent(); BEGINNERSWORD.BEGINNERSWORD_01 bs1 = GetComponent(); Weapon = bs1._MODEL; } } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles


Practice Sheet of Right form of verbs for HSC Students


Rajasthan Board 10th Result 2016 Roll No wise & Name Wise


Moondru Mudichu 20-07-2016 – Polimer tv Serial


fs_older_downloads


Calaveras conflict results in shooting, 4 arrests


Guilty Pleasures


Black Angus Grilled Artichokes


Download: Rich Bizzy -Panono Ukwenda (Cover)


Muloraki Au


where in jaunpur randi khana



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>