Daniel Lip
Daniel Lip

Reputation: 11325

Why when starting the game the script is turned off ? I need to turn it on after the game is running

I have a prefab name bullet. I dragged the prefab and put it under a weapon. The script is turned on but when starting the game the script is turned off and I need to turn it on again.

In the screenshot the bullet prefab is in the middle. On the left in the hierarchy the bullet is under SciFiRifle(Clone) on the right in the Inspector on the bottom there is a script attached name Shooting.

The script is turn on.

But then when I'm running the game the script for some reason is turned off : First for some reason I also need to collapse the SciFiRifle(Clone) to see the bullet again in the hierarchy :

Need to collapse

And the script is turned off :

Turned off

I can't figure out why it's hiding the bullet in the hierarchy and turning the script off when running the game.

The script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public GameObject projectile;
    public Animator anim;

    private void Start()
    {
        anim.SetBool("Shooting", true);
    }

    private void Update()
    {
        if (anim.GetBool("Shooting") == true)
        {
            GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
            bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10, ForceMode.VelocityChange);
        }
    }
}

UPDATE :

I found what is making the problem. Not yet how to solve it. The second child in the Hierarchy of Object is Sci-Fi_Soldier.

And to Sci-Fi_Soldier attached some scripts one of them is Player Controller it's not my script.

Some properties of this script in the Inspector get Right Gun what i assigned in to the Right Fun is the SciFiRifle(Clone) and the Bullet is child of SciFiRifle(Clone).

You can see it marked in yellow :

Gun

If I'm removing this script at all the Player Controller then the script Shooting on the Bullet will stay turned on and it will stay collapsed.

I can't figure out what making the problem in the Player Controller script :

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Animator))]
public class PlayerController : MonoBehaviour {

    public Transform rightGunBone;
    public Transform leftGunBone;
    public Arsenal[] arsenal;

    private Animator animator;

    void Awake() {
        animator = GetComponent<Animator> ();
        if (arsenal.Length > 0)
            SetArsenal (arsenal[0].name);
        }

    public void SetArsenal(string name) {
        foreach (Arsenal hand in arsenal) {
            if (hand.name == name) {
                if (rightGunBone.childCount > 0)
                    Destroy(rightGunBone.GetChild(0).gameObject);
                if (leftGunBone.childCount > 0)
                    Destroy(leftGunBone.GetChild(0).gameObject);
                if (hand.rightGun != null) {
                    GameObject newRightGun = (GameObject) Instantiate(hand.rightGun);
                    newRightGun.transform.parent = rightGunBone;
                    newRightGun.transform.localPosition = Vector3.zero;
                    newRightGun.transform.localRotation = Quaternion.Euler(90, 0, 0);
                    }
                if (hand.leftGun != null) {
                    GameObject newLeftGun = (GameObject) Instantiate(hand.leftGun);
                    newLeftGun.transform.parent = leftGunBone;
                    newLeftGun.transform.localPosition = Vector3.zero;
                    newLeftGun.transform.localRotation = Quaternion.Euler(90, 0, 0);
                }
                animator.runtimeAnimatorController = hand.controller;
                return;
                }
        }
    }

    [System.Serializable]
    public struct Arsenal {
        public string name;
        public GameObject rightGun;
        public GameObject leftGun;
        public RuntimeAnimatorController controller;
    }
}

It's hiding the Bullet in the Hierarchy and also turning off the script Shooting. But the script Shooting is not used anywhere else in the Hierarchy or in any other scripts. But by trying removing scripts I found that this script is the one that make the problem with the Bullet.

This is the Inspector screenshot of SciFiRifle(Clone) :

SciFiRifle(Clone)

Upvotes: 0

Views: 183

Answers (1)

Saeleas
Saeleas

Reputation: 548

The PlayerController script actually destroys your Sci-fi Rifle (the one you put in the prefab) and then instantiates a new one with the one provided in the Right Gun field of the PlayerController script (that is why you need to collapse it again, because what you see is a new instance of the rifle prefab).

The lines in the PlayerControllerthat describe this behaviour are:

                if (rightGunBone.childCount > 0)
                    Destroy(rightGunBone.GetChild(0).gameObject);

Which destroys the rifle you put in the prefab. And:

                if (hand.rightGun != null) {
                    GameObject newRightGun = (GameObject) Instantiate(hand.rightGun);
                    newRightGun.transform.parent = rightGunBone;

Which spawns a new instance of the prefab you provided in the Right Gun field, replacing the rifle you put in the prefab.

You could try: either removing the prefab from the PlayerController field or remove the Sci-fi Rifle from the prefab.

Upvotes: 1

Related Questions