0

Unity newb here :)

I have a button which calls this function which basicly sets a prefab to instantiate.

  public void setTurret()
{
    towerNode.setTurretType(turret)
    Debug.Log("selected shop turret:" + turret.name);
}

My Tower_Node class handles the actual instantiate

 public class Tower_Node : MonoBehaviour
    {
        private GameObject turret;

               public void setTurretType(TowerType _turret) 
                   {
                        turret= _turret.prefab;
                    }



               private void OnMouseDown()
               {             
                Instantiate(turret,GetBuildPosition(),Quaternion.identity);
               }
    ...}

EDIT: What I also tried

      public class Tower_Node : MonoBehaviour
        {
            private TowerType turret;

                   public void setTurretType(TowerType _turret) 
                       {
                            turret = _turret;
                    }



               private void OnMouseDown()
               {             
                Instantiate(turret.prefab,GetBuildPosition(),Quaternion.identity);
               }
    ...}

EDIT: This is how the references in the inspector look. The shop script is the script with the setTurret() method

--

Setting the turret with the setTurretType method works. If i check it with Debug.Log() i get the right TowerType but outside of the function the gameobject is still =null and when i try to instantiate it gives me an NullReferenceException (because Gameobject is null obviously)

What am i missing here?

Thank you for your answers.

2
  • Is it possible you have multiple instances of Tower_Node but you do not use setTurretType on all of them? Commented May 20, 2020 at 15:05
  • My Tower_Node is a prefab, which exists multiple times in my scene. setTurretType should update all of them since its a prefab right? Commented May 21, 2020 at 13:08

2 Answers 2

0

switch OnMouseDown() for

if (Input.GetMouseButtonDown(0))

in your update loop

just make sure that turret.prefab is a prefab

Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
Sign up to request clarification or add additional context in comments.

1 Comment

This did not work, and yes im sure that the prefab is a prefab!
0

I would say that if your TowerType class has a GameObject var named prefab, you should either assign the prefab to the prefab, or assign the turret to the _turret and the access the prefab.

Either this:

public void setTurretType(TowerType _turret) {
    turret = _turret;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

or

public void setTurretType(TowerType _turret) {
    turret.prefab = _turret.prefab;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

should work.

Edit: You could set the turret while instiantiating:

public void setTurretType(TowerType _turret) {
    turret.prefab = _turret.prefab;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    turret = Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

5 Comments

Yes my TowerType class have a public GameObject prefab. I tried both, but unfortunaly this did not work, the object still won't update
Are you setting the prefab to null somewhere in your code?
No, im not thats the only piece of code which modifies that
Sadly this also did not work, even when i set the var to [SerializeField] this does not work, but i can see in the inspector how the actual prefab is swapped. I really dont get why its null
Then I would say you need to provide more information with your other classes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.