Hello, I’ve searchd a lot for this but had no luck so far.
I’m new in this stuff. I have Bullet as a Prefab and every time new bullet Instantiates i want it to access “PlayerScript” (Which is attached to gameObject “Player”), here goes what i tried
using UnityEngine;
using System.Collections;
public class BulletScript : MonoBehaviour {
public float duration = 1;
public float bulletSpeed = 10;
public PlayerScript PlayerScript;
public GameObject Player;
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
PlayerScript = Player.GetComponent<PlayerScript>(Player);
}
// Update is called once per frame
void Update ()
{
transform.Translate (Vector3.forward * bulletSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Red") {
PlayerScript.redHit++;
}
else if (other.gameObject.name == "Green") {
PlayerScript.greenHit++;
}
//Debug.Log (redHit);
Destroy (gameObject.gameObject);
}
}
Main thing there is Function Start, don’t pay too much attention to other stuff.
I’ve tried
GameObject.FindWithTag
and
Player = GameObject.Find(“Player”);
it did nothing good.
Any help?
C# would be great.