- Home /
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.
Answer by Tricky7991 · Jun 25, 2017 at 01:36 PM
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;
//Instantiate(trackLink);
}
}
Again thank you @TonyLi, and sorry though for it being something stupid.
Answer by TonyLi · Jun 23, 2017 at 01:08 PM
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>();
I tired : Script word1 = GameObject.Find("Object").GetComponent<Script>();
"The type or namespace name `Script' could not be found. Are you missing an assembly reference?'
I have a fresh brand new project with nothing but my files in it no Standard Assets. What I am doing is basically. I have a gameobject that spawns another gameobject. And when it does so, its suppose to access another object and add to the count I have everything else down. And Ive done this before but now I seem to no be able too.
Sure here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrackSpawner : $$anonymous$$onoBehaviour {
//This is the track object we are instantiating.
public GameObject trackLink;
// Use this for initialization
void Start ()
{
Script track1 = GameObject.Find("Track$$anonymous$$anager").GetComponent<TracksInfo>();
if (track1.LeftSideTrack <= 79)
{
track1.LeftSideTrack += 1;
Instantiate(trackLink);
};
}
// Update is called once per frame
void Update ()
{
}
}
Your answer
Follow this Question
Related Questions
Is there a reason why Unity would be able to read one variable in a script and not another? 1 Answer
After exporting my project from my PC to my laptop the physics of my game work differently. 1 Answer
Left side panel in monodevelop 1 Answer
Script problem 1 Answer
How to prevent an object from instantiating inside another object? 1 Answer