I cant turn off my mesh renderer for some reason

Hi Everyone,

I am having a problem turning my mesh off when my player dies. I have my networking going.. it is detecting collisions and triggering the explosion but I cant get it to turn off... here is what I have so far.

[RPC]
public void KillThisPlayer()
{
    myState = State.dead;
    gameObject.renderer.enabled = false;
    turret.gameObject.renderer.enabled = false;
    StartCoroutine(DestroyTank());
}

[RPC]
public void TurnOffThisRenderer()
{
    //MeshRenderer rendered = (MeshRenderer)gameObject.GetComponent("Tank").GetComponent("MeshRenderer");

    //MeshRenderer turret = gameObject.GetComponent("Turret").GetComponent<MeshRenderer>();

    //rendered.renderer.enabled = false;
    //turret.enabled = false;

    //These objects are declared as Transforms at class level and they are found and //used throughout the code
    turret.renderer.enabled = false;
    stvol.renderer.enabled = false;
}

IEnumerator DestroyTank()
{

    Network.Instantiate(ExplosionPrefab, myTransform.position, Quaternion.identity,0);

    turret.renderer.enabled = false;

    GameObject[] spawnpoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
    Debug.Log("Spawns: " + spawnpoints.Length);

    Transform spawnPoint = spawnpoints[Random.Range(0, spawnpoints.Length)].transform;

    yield return new WaitForSeconds(respawnTime);
    myTransform.position = new Vector3(spawnPoint.position.x, 1, spawnPoint.position.z);
    gameObject.renderer.enabled = true;
    turret.renderer.enabled = true;
    myState = State.playing;
    myHealth = 100;
}

public void DecreaseHealth(int value)
{
    //decrease it by the amount of damage the projectile does
    if (myState == State.dead || myState == State.invincible)
        return;
    if (Network.isClient)
        return;
    Debug.Log(Network.isServer ? "we are a server" : "we are a client");
    myHealth -= value;

    if (myHealth <= 0)
    {
        Debug.Log(userName + " is dead");
        //Instantiate(ExplosionPrefab, myTransform.position, Quaternion.identity);
        networkView.RPC("TurnOffThisRenderer", RPCMode.AllBuffered, null);
        networkView.RPC("KillThisPlayer", RPCMode.All,null);
    }
    else
        Debug.Log(userName + " has " + myHealth + " health left.");

The coroutine and everything works fine even on the networked computers, as it finds the spawnpoint and respawns like it should.

I have tried many ways to get to the meshrenderer but it keeps saying it is nullreference. I just dont know how to get to it I suppose.

I have my prefab set up like this

  • Player
  • Tank (this is the tank body)
  • Turret (turret sits on top of the body)
  • Camera / text object.

I have ensured that the player object has a mesh renderer, the Tank and Turret object do as well. Anyone see anything wrong with what I am doing? or why it will not disappear on either the server or client?

Any help appreciated.

Mike

gameObject.GetComponent(MeshRenderer).enabled = false;

That might help.

Edit: Just noticed you asked the question over 2 years ago! You’ve probably already figured it out. :smiley: Unless I just saved you from eternal damnation. :stuck_out_tongue:

Did you try

transform.Find("turret").GetComponent("MeshRenderer").enabled = false;

Of course, you can!