Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Accessing Instantiated prefab's script

Discussion in 'Scripting' started by vargata, May 2, 2021.

  1. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    I have the below codes on 2 objects, one is a prefab just been instantiated and I can't seem to find why its not working. Could you guys please take a look for me? I had to cut it up to so many lines to check everything, and the myNewBaseScript is always null
    cheers
    Tom

    Code (CSharp):
    1.  
    2. GameObject myNewBase;
    3.  
    4. myNewBase = Instantiate(myPrefab, hit.point, Quaternion.LookRotation(2 * (this.transform.position - targetPoint)), this.transform);
    5.  
    6. if(myNewBase != null) {
    7.  
    8.    SelectBase myNewBaseScript;
    9.    myNewBaseScript = (SelectBase) myNewBase.GetComponent("SelectBase");
    10.    myBases.Add(myNewBaseScript);
    11.  
    12.    if(myNewBaseScript != null)
    13.       myNewBaseScript.myBaseId = myBases.Count;
    14.  
    15.    Debug.Log(myNewBaseScript.myBaseName);
    16. }

    Code (CSharp):
    1. public class SelectBase : MonoBehaviour
    2. {
    3.     int BaseId = 0;
    4.     string BaseName = "New Base";
    5.  
    6.     public GameObject myRenderer;
    7.  
    8.     [HideInInspector] public string myBaseName {
    9.         get { return BaseName; }
    10.         set { BaseName = value; }
    11.     }
    12.  
    13.     [HideInInspector] public int myBaseId {
    14.         get { return BaseId; }
    15.         set { BaseId = value; }
    16.     }
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    I think line 9 should be:
    Code (CSharp):
    1. myNewBaseScript =  myNewBase.GetComponent<SelectBase>();
     
  3. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    Thanks, but sadly that's the exact same thing... and throws the exact same nullref exception :(
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    If you're getting an exception, always post the exact error message and indicate which line it occurs on.

    When you said "myNewBaseScript is always null" I assumed you meant that your code was skipping the "if" block, which would not result in an exception. Sounds like something else is going on.

    But if the problem really is that myNewBase.GetComponent is returning null, then that's probably an issue with your prefab, not your code.
     
    vargata likes this.
  5. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    yes, it skips the if but if i run it without the if, the line where I use the myBaseId throws the exception.

    found the error right now and I was just stupid, sry about it. the script was attached to the child gameobject instead of the baseobject of the prefab so I have to look for it with GetComponentInChildren. Now Im banging my head against the wall wasting half a day for this :( I debugged everything except the name of the myNewBase, that would help me to realise this quicker...

    thanks for looking in and still trying to help, appreciate it. it runs now perfectly... o_O