How to access scripts from other gameObjects

I’m having considerable trouble with passing information between two scripts stored in two gameObjects.

I want my player class to store information in a script in a prefab instance when they enter a collision, and retrieve it when they leave a collision.

I’ve been looking around for awhile and have found solutions like:

  • GameObject go = GameObject.Find(“somegameobjectname”);
  • ScriptB other = (ScriptB) go.GetComponent(typeof(ScriptB));
  • other.DoSomething();

But my script name (StaticObjectController) gives the compiler error:

The type or namespace name ‘StaticObjectController’ could not be found…

I don’t know what this means, and other people don’t seem to be having this problem.
Im using C#.

According to this error you don’t actually have a script called StaticObjectController. Is it spelt exactly right? You need all the capital letters to be identical.

Yes, that is the exact spelling copied over. I’m not sure how scope applies in this case, but does the gameObject hierarchy have any impact? The two gameObjects in question are on the same level, one is not the child of another.

For reference

public class playerController : MonoBehaviour {

public StaticObjectController temp;

void Start () {

}

}

and…

public class StaticObjectController : MonoBehaviour {

public string playerColSide;

void Start () {

}

}

The error is a compiler error. That means there is a problem in the way the code itself is written. Unity hasn’t actually tried to run it yet, so the problem is not in the hierarchy.

Do you mind posting the exact code you are using, along with the error message and line number?

Aparently, after restarting my Visual Studio to get the code, it got the idea and stopped giving the error. Sorry to waste your time.

That was going to be one of my other suggestions if there was no problems in the code. Sometimes Unity doesn’t quite import things properly. If the code is fine, sometimes a re import can fix it.

Glad you got it working.