Obtaining data from selected object - c#

I'm looking for a way to obtain data from an object I have "selected/targeted" in game. Example: I want to select an enemy to attack and then take the hp value from the script attatched to them and compare it to the attack value on the player script. I've looked around on google and youtube but have found no luck. Hoping someone could help me or point me to a guide to look at.
Example in code form:
Script 1:
public class Enemy : MonoBehaviour
{
public int enemyHealth;
}
Script 2:
public class Player : MonoBehaviour
{
public Enemy enemy;
public int playerAtk;
}
public void Attack()
{
"Selected enemy's enemy script".health -= playerAtk;
}

You must access the enemy through a specific event. For example, when hitting a bullet or clicking the mouse on it. Below are some examples of how to get the enemy, but keep in mind that there are countless ways to access the enemy.
On Trigger Enter
This is a very simple way, This code works in the player class. The collider key gives you access to the enemy, now you can access the methods by holding its component.
public void OnTriggerEnter(Collider other)
{
var _enemy = other.GetComponent<Enemy>(); // // Get Enemy class
_enemy?.Damage(10); // damage method run.
_enemy?.Damage(this, 10f); // Send player to method and cause damage
}
Physics Raycast
This will happen by throwing a ray from the camera, what the raycast code does is return the point of impact in raycastHit format. After getting it, you can access your other raycastHit components as shown below.
public void Update()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Input.GetKeyDown(KeyCode.Mouse0)) return;
if (Physics.Raycast(ray, out var RaycastHit))
{
var _enemy = RaycastHit.transform.GetComponent<Enemy>();
_enemy?.Damage(10);
}
}
Specify in the Inspector
In this method, you can put all the enemies in an array and damage them by calling the index.
public Enemy[] myEnemy;
public void Damage(int index)
{
myEnemy[index].Damage(10);
}
Find By Type
Another popular method is to capture all enemies and filter them based on a specific condition. This method is also performed in this way.
var enemies = FindObjectsOfType<Enemy>();
foreach (var _enemy in enemies.Where(e => e.health >= 40f))
{
_enemy.Damage(100);
}

Related

How do i add the objects that the raycast is hitting in unity and add them to a list

I have this problem that i just can't figure out how to solve.
I am trying to make a game in Unity, and i have stumbled across a problem that goes like this.
I want to put an object in the scene in a list, when i hit it with a raycast.
With what i have tried so far. Either it puts everything that is tagged the same thing on the list when i press mousebutton on one of the objects, or it only puts in the same thing (Cube in this example).
My code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTagged : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
public List<GameObject> playersTagged;
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(damage);
if(hit.collider.tag == "Taggable")
playersTagged.Add(GameObject.FindWithTag("Taggable"));
}
}
}
}
use
playersTagged.Add(hit.collider.gameObject);
Instead of using GameObject.FindWithTag() which returns the first object it'll find source
Use gameobject property playersTagged.Add(target.gameobject). Since all components store the reference to the gameobject they are attached to.
You are adding GameObject.FindWithTag("Taggable") which means you are getting any GameObject that has that tag and adding it to the list. But instead, you want to add the hit object, so you need to add hit.collider.gameObject.

I don't know how to communicate with other scripts

I have a Player script. I have the variables on there
public class Player : MonoBehaviour
{
float horimove;
Rigidbody rb;
public float speed;
public GameObject thrown;
public bool win;
On my other script (I'm making a game where you instantiate spheres into a hole and win.)
public class spheresciprt : MonoBehaviour
{
public Player pscript;
public GameObject player;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
pscript = player.GetComponent<Player>();
}
private void OnTriggerEnter(Collider other)
{
if (gameObject.CompareTag("win"))
{
pscript.win = true;
}
}
However pscript.win doesn't change. The bool always stays false but I want the player to win after the sphere hits the trigger in the hole I want it to go to. I tried different computers. The same thing happened in different games I think so I'm probably just missing something.
First of all, make sure that the collision is detected. You can easily do something like
void OnTriggerEnter()
{
Debug.Log("Pass1");
if (gameObject.CompareTag("win"))
{
Debug.Log("Hey, it worked!");
}
}
in the OnTriggerEnter to check that.
If the collision is NOT detected, make sure there are colliders on all the affected GameObjects, as well as a RigidBody component on the sphere. Also, make sure that the collider is marked as Trigger. If you still have an issue with that, we will need to see your inspector to figure it out.
If the collision IS detected... Well, start by making sure that you have assigned the proper tag on the hole. Then, make sure that the player GameObject is actually called "Player" (since you are trying to find it by name) and that it has the script you are trying to get on the object you actually find (and not on one of its children). I hope that works, because I can't see anything else seriously flawed at your code.
Have you tried to change
if (gameObject.CompareTag("win"))
to
if (collider.gameObject.CompareTag("win"))
or just add the rigidbody to both player and other object.
does the other object have "win" tag?
Here is your script:
public class spheresciprt : MonoBehaviour
{
public Player pscript;
public GameObject player;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
pscript = player.GetComponent<Player>();
}
private void OnTriggerEnter(Collider other)
{
if (gameObject.CompareTag("win"))
{
pscript.win = true;
}
}
}
It sets pscript to the current values in Player script. This is the problem, because it is setting a variable to the current values, and you want access to the component. You could change this by, just replace all of the pscript variables with player.GetComponent<Player>(). There are other ways of doing this. You could, at the end of OnTriggerEnter, add this:
void OnTriggerEnter(Collider other)
{
...
player.GetComponent<Player>() = pscript;
}
In both of these solutions you fix it. In one, you re-apply the variable. In the other, you are changing the component directly. Which is what you want, because you have pscript as an instance of player script, not the player script that is attached to player.

How to tell which of the colliders has been collided?

I'm creating a game in which there are enemies, I want to have headshots in the game so I have 2 colliders: one to the head and one to the body. I can't find any good way to tell which is which in the code.
I thought of a solution but I don't like it- a different type of collider to the head, and different type to the body (like polygon and box colliders). It works but I don't think it's good enough (if I want to add more colliders or have two of the same type that wouldn't work).
virtual protected void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.gameObject.tag.Equals("Zombie"))
{
Destroy(gameObject);//destroy bullet
Zombie zombie = collider2D.gameObject.GetComponent<Zombie>();
if (collider2D is BoxCollider2D)
zombie.HeadShot(demage);//headshot
else zombie.BulletHit(demage);//normal hit
}
}
I want a way to tag the colliders somehow so I can tell between them.
You need to create public variables of type BoxCollider2D and assign your colliders. When a collision occurs call an IF statement inside of OnTriggerEnter to see which one has collided. This will work no matter if there are more of the same types of collider.
public class Example : MonoBehaviour
{
public BoxCollider2D box01;
public BoxCollider2D box02;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.IsTouching(box01))
{
Debug.Log("1");
}
else if(collision.IsTouching(box02))
{
Debug.Log("2");
}
}
}
isTouching is a Unity method which returns a bool depending on a collider that is comparing.
I would suggest to not add all colliders on the same GameObject but rather give each collider it's own child GameObject (this way you also can see easily which colliders belongs to which outline in the scene view ;) )
Then you could use a class with an enum to define which type of collider you have there:
public class BodyPart : MonoBehaviour
{
public BodyPartType Type;
}
public enum BodyPartType
{
Head,
LeftArm,
RightArm,
Body,
LeftLeg,
RightLeg
}
and attach it to all body parts next to each collider.
Then you could do something like
virtual protected void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.gameObject.tag.Equals("Zombie"))
{
Destroy(gameObject);//destroy bullet
// Note you then should use GetComponentInParent here
// since your colliders are now on child objects
Zombie zombie = collider2D.gameObject.GetComponentInParent<Zombie>();
var bodyPart = collider2D.GetComponent<BodyPart>();
switch(bodyPart.Type)
{
case BodyPartType.Head:
zombie.HeadShot(demage);//headshot
break;
// you now could differ between more types here
default:
zombie.BulletHit(demage);//normal hit
break;
}
}
}

How to make an object clickable depending on player distance?

I need to make an item in my scene clickable but only when the player is near the item. In my script I make the item in question go automatically to an empty GameObject that is child of my Player in the hierarchy to define the position but the click is able as soon as the camera have it framed. I'm using the character controller provided in the 2d physics and not a 2drigidbody so I'm even more confused because I can't use a collider.
I'm pretty sure you can have both a character controller and a collider on a gameobject (at least a trigger collider).
Then instead of whatever you're using to detect the click, you should use in an Update loop something like Input.GetKeyDown(KeyCode.Mouse0), and use a raycast where you can specify the length of the ray. https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
In order to make anything clickable I would recommend IPointerXHandler interfaces (replace X with Click, Enter, Exit, Down, Up, etc).
Note:
Ensure an EventSystem exists in the Scene to allow click detection. For click detection on non-UI GameObjects, ensure a PhysicsRaycaster is attached to the Camera.
If you only want to click IPointerClickHandler is enough. If you want some visiual feedback like changing colors etc you'll have to expand it with at least IPointerEnterHandler and IPointerExitHandler.
public class MyClickable : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData pointerEventData)
{
...
}
}
Then in order to get the distance between two obects you can simply use Vector3.Distance e.g. with a ceratin threshold
// configure those e.g. in the Inspector
public float distanceThreshold;
public Transofrm playerTransform;
public Transform itemTransform;
and than use something like
if(Vector3.Distance(playerTransform.position, itemTransform.position) <= distanceThreshold)
{
...
}
so if you directly implement this into the MyClickable you could do visual feedback also in Update something like
public class MyClickable : MonoBehaviour, IPointerClickHandler
{
public float distanceThreshold;
public Transofrm playerTransform;
// this gives you an event you can configure in the Inspector
// exactly like you would with a button
public UnityEvent onClick;
private bool isInRange;
public void OnPointerClick(PointerEventData pointerEventData)
{
// if too far away do nothing
if(Vector3.Distance(playerTransform.position, transform.position) > distanceThreshold) return;
....
onClick.Invoke();
}
private void Update()
{
if(Vector3.Distance(playerTransform.position, transform.position) <= distanceThreshold)
{
// e.g. make object green
}
else
{
// e.g. make object grey
}
}
}
My suggestion is to use the onMouseDown() method. If you have a collider or trigger attached to your gameObject, onMouseDown() will detect mouse clicks on the object. Then in then body of onMouseDown() you can test if it is in range.
This is an example script how it could work:
public class ItemClickable : MonoBehaviour
{
public Transform player; // player-transform reference (depends if you have a singleton or not)
public float range; // radius (maybe you have a range or radius already set in your player instance)
void Start()
{
// Setup for your references
}
private void OnMouseDown()
{
// Checks if the item is in the range of the player
if ((player.position-gameObject.transform.position).magnitude < range) // Vector3.Distance() is also possible
{
Destroy(gameObject); // or do whatever you want in here
}
}
}

Editing variables with GetComponent C#

I am trying to get a conveyor belt to reverse its direction when the player presses a button.
Here's the code for the conveyor belt
using UnityEngine;
using System.Collections;
public class Conveyor : MonoBehaviour {
public float speed = 1.0f;
void OnTriggerStay(Collider col)
{
col.transform.position += transform.forward * speed * Time.deltaTime;
}
}
And the code for the button
public class PushButton : MonoBehaviour
{
public GameObject Button;
private Conveyor conveyor;
void Awake ()
{
conveyor = GetComponent<Conveyor>();
}
void OnTriggerStay(Collider entity)
{
if (entity.tag == "Player")
{
if (Input.GetKeyUp(KeyCode.E))
{
conveyor.speed = conveyor.speed * -1;
}
}
}
}
I'm getting an error saying "Object Reference not set to the instance of an object PushButton.OnTriggerStay (Unity Engine.Collider entity) (at Assests/PushButton.cs21)
I'm still not very familiar with using getComponent so I'm not sure how to fix this. Any help would be appreciated.
GetComponent will need a reference to an initialized object and a component or class that object has assigned. For what you are wanting, you will want to find a game object in the scene. Because you are already specifying that your GameObject has a Conveyor class assigned to it, find your GameObject and then specify the Conveyor component.
void Awake ()
{
conveyor = GameObject.FindWithTag("Conveyor").GetComponent<Conveyor>();
}
This should do the trick pending you have tagged your Conveyor game object with a 'Conveyor' tag.
However, there is an even easier way to quickly "grab" something like this. Be careful though!
void Awake()
{
conveyor = Object.FindObjectOfType<Conveyor>();
// ONLY DO THAT IF THERE IS ONLY >>ONE<< OF THE THING!
}
Here's a little essay on that. http://answers.unity3d.com/answers/46285/view.html
You often do that for example to find "boss" objects .. scene managers and the like.
Don't forget though, you can always just use a public variable, and drag inteh Inspector - that's always a sound idea for beginners working with Unity.

Categories

Resources