#Tiny issue

1 messages · Page 1 of 1 (latest)

prisma anchor
#

Hello everyone, I am currently working on a tank shooter game as a project to boost my skills. So I have encountered a tiny problem.

#

When I get the mouse button (0) down, everything works except for the bullet's physics.

#

It goes backwards instead of going bullet speed front

#

Here's the bullet prefab components & its settings:

#

The scripts:

#
{
    if (Input.GetMouseButtonDown(0))
    {
        shootFX.Play();

        
        GameObject bullet = Instantiate(bullet_prefab, bullet_start_point.position, bullet_start_point.rotation);
        bullet.GetComponent<Bullet>().Move(2000f);

    }
}```

```using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public class Bullet : MonoBehaviour
{
    [SerializeField] public Rigidbody rb;

  

    public void Move(float speed)
    {
        rb.AddForce(transform.forward.normalized * speed);
        Invoke("DeactivateGameObject", 4f);
    }

    void DeactivateGameObject()
    {
        gameObject.SetActive(false);
    }
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Obstacle")
        {
            gameObject.SetActive(false);
        }
    }

}
surreal obsidian
#

You are applying a Force that is supposed to be applied over time, but you only apply it once. You want to use ForceMode.Impulse to apply a "one-time" force

#

There's also some redundant writing in your code, you've got a SerializeField attribute on a field that already is public. First off don't use public at all, make all fields private and expose them in the editor with the SerializeField attribute. If other scripts needs to access a member then a good practise is to use public properties instead.

#

One last note, since you probably are gonna fire alot of bullets, make a recycle system for the bullets so you don't have to instantiate them, as instantiating is bad for the performance

prisma anchor
#

For the past few hours I've been trying to understand the issue regarding why the zombies and the obstacles aren't spawning. The error is an object reference not set to instance of an object. I tried to make the private base controller public & attached the game object I'm controlling. However despite all that, I am unable to get the solution.

#

here's where the issue lies exactly:

{
    HalfGroundSize = GameObject.Find("GroundBlock Main").GetComponent<GroundBlock>().HalfLength;
    PlayerController = GameObject.FindGameObjectWithTag("Player").GetComponent<BaseController>();
    StartCoroutine("GenerateObstacles");
}```
#
{
    float timer = Random.Range(minDelay, maxDelay) / PlayerController.speed.z;
    yield return new WaitForSeconds(timer);
    CreateObstacles(PlayerController.gameObject.transform.position.z + HalfGroundSize);
    StartCoroutine("GenerateObstacles");
}```
surreal obsidian
prisma anchor
surreal obsidian
#

Now is a good example to why you shouldn’t use it: due to its high errorprone.

  1. you need to check that the string name is correct
  2. You need to make sure that the tag exists
  3. You need to make sure that the object you are trying to reference has the tag
  4. if you have several objects with the same tag, there’s no guarantee to get the object you want

There’s other, more reliable ways to get references :)

clear magnet
#

Funnily enough, since the error comes from line 36, it appears that there is an object found with the tag, but that object doesn't have a BaseController component attached kekw
Which I guess corroborates point 4.

surreal obsidian
#

Which makes me also want to mention that OP should provide footage that people on phone can access x) I can’t see the video where you probably saw the log