Cant access another script

So i just made this script

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

public class TechButton : MonoBehaviour {

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

that one is added to prefab of button so each button has it.
on another game object named controller there is researchController script which needs to call every button from list of spawned buttons like this

for (int i = 0; i < techButtons.Count; i++)
        {
            techButtons[i].GetComponent<TechButton>().UpdateButton();
        }

techButtons is list of List and it just doesnt recognize TechButton class from anywhere on script like any other script, why i cant access it as it worked before just fine.

thanks

Your TechButton class doesn’t have an UpdateButton method.

Yeah i know this was new one which i tried to create again. One before it had. Still it would need to find public class TechButton

What do you mean? What is the issue you are facing?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TechButton : MonoBehaviour {
 
    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
       
    }

public void UpdateButton(){
// do something here
}
}

also… you should never assume something is there…

		for (int i = 0; i < techButtons.Count; i++)
        {
            if(techButtons[i] != null){
				var button = techButtons[i].GetComponent<TechButton>();
				if(button != null){
					button.UpdateButton()
				}
        }

lastly… I would not store gameObjects, when all you ever need is a monobehaviour from that object…

public TechButton[] techButtons;

it shortens the processing needed to get each element as you go, making your game just a touch faster.

1 Like

Still really early in development so dont really pay much attention to checking.

Also like idea of just storing but i would access to name of object in that script.

Yeah, problem is that program doesnt recognise TechButton. Its marked red. Says it couldnt find it. Also intellisense doesnt recommend it but does others.

does something here use a namespace?

using a namespace would do you in.

Perhaps post the script in which you have pulled the seconds set of code from.

Try all the standard fixes first. Reimport the script in Unity. Restart Unity. Make sure the script name matches the class name. (Script name is TechButton and not techButton or something like that). Those would be the first things I’d try.

2 Likes

Sorry for inconvinience. Restarting unity fixed it. When quitting it asked me to save name of my project. Maybe saved my solution or something. Anyway thanks for the help