- Home /
Question by
woleklolek123 · Oct 04, 2019 at 09:16 PM ·
scripting problem
Colliding errors
So i want a value setted to a variable whenever it will collide with good object. my first script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PowerUp : MonoBehaviour
{
public new Collider collider;
public void OnCollisionEnter(Collision collision)
{
var armor = gameObject.GetComponent<Inventory>();
if (collider.CompareTag("GoodCollider"))
{
System.Console.WriteLine("Hitted good collider");
armor.getArmor("Helmet");
}
}
}
My second script: using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class Inventory : MonoBehaviour
{
public bool isOpen = true;
public GameObject EQ;
public string currentArmor = "none";
public void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (isOpen == true) Close();
else if (isOpen == false) Open();
}
}
public void Open()
{
EQ.SetActive(true);
isOpen = true;
}
public void Close()
{
EQ.SetActive(false);
isOpen = false;
}
public void getArmor(string armorType)
{
currentArmor = armorType;
}
}
(to value armortype)
Comment
I didn't understand everything but try
public void OnCollisionEnter(Collision collision)
{
var armor = collision.gameObject.GetComponent<Inventory>();
if (collider.CompareTag("GoodCollider"))
{
Debug.Log("Hit good collider");
if( armor != null )
armor.getArmor("Helmet");
else
Debug.LogError("No armor found on other gameObject " + collision.gameObject.name, collision.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Unity Ads Banner not working. 0 Answers
Building System problem 0 Answers
How to change a Vector3 list individual axis using for statement 1 Answer
TANKS! both firing with spacebar 0 Answers