Namespace Problem

I know this has been asked and answered more then one could count. But everything I have looked hasn’t helped. I know its probably something simple. Anyways…

Word word1 = GameObject.Find("Object").GetComponent<Script>();

I am trying to access another game objects script and set a value. But I keep having the problem that

"The type or namespace name `Word’ could not be found. Are you missing an assembly reference?’

Can someone shed light on this? I was reading somewhere that maybe I needed something else in the “using” at the top of the script?

Thanks in advance I haven’t used unity in a few years so Im getting back into it.

This means a class named Word isn’t in your project, or it’s in a namespace that you haven’t included with a “using” statement.

Since you’re getting a component of type Script, try this:

Script word1 = GameObject.Find("Object").GetComponent<Script>();

It declares that word1 is a variable of type Script instead of type Word.

If Script is a subclass of Word, then you could use your original line of code as long as Word is included with a using statement. In this case, find the script file that contains the definition of Word. Check if the class is wrapped in a namespace, such as:

namespace Foo {
    public class Word : MonoBehaviour { ... }

If so, then add a line like this to your original script:

using Foo;

Side note: It’s a good idea to check all return values before using them. GameObject.Find(“Object”) may return null, in which case you can’t call GetComponent on it. This is safer code:

var go = GameObject.Find("Object");
if (go == null) Debug.LogError("Can't find GameObject named Object");
else Script word1 = go.GetComponent<Script>();

First off I’d like to thank you @TonyLi for the help. You got me thinking and going at this all ways possible.

It was my fault it wasn’t working. The scripts were never compiling because of the errors. Which means it couldn’t find the script because it never existed yet. Simply rid of the errors and let it compile the other scripts then I was simply able to use this code and it works perfect.

// Use this for initialization
	void Start () 
		TracksInfo tracksInfo = GameObject.Find("TrackManager").GetComponent<TracksInfo>();

		if (tracksInfo.LeftSideTrack <= 79)
			tracksInfo.LeftSideTrack += 1;


Again thank you @TonyLi, and sorry though for it being something stupid.