access script from other gameobject

abb

I have this script called abb with the following code

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


public class abb : MonoBehaviour
{
public GameObject test;
myScript  aaa;

    void Start()
    {
        myScript = test.GetComponentsInChildren<aaa> ();    
     
    }

}

And codenamed aaa
with the following simple code

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

public class aaa : MonoBehaviour
{
int a;
   
    void Start()
    {
        a=1;
    }


}

I am trying to be able to see the value of a from script aaa in script abb but all I get is a namespace error. I can’t even attach the scripts to Gameobjects.What am I missing?

Here you write “MyScript” then “MYScript”, did you perhaps make the same mistake in your code?

Nope sorry for the mistake

I have using system.Collections and using Systems.collections.generic greyed out at the top a mouse over reports uneccessary using directive.Using vscode

is the script attached to a gameojbect. If so you need to reference the game object first.

GameObject go = GameObject.Find(the gamobject name);

MyScript script  =  go.GetComponent<MyScript>();

Tried that but it gives the same error.

 GameObject camp = GameObject.FindGameObjectWithTag("MainCamera");
myscript script = camp.GetComponent<alreadyclicked>();

Can you maybe write 2 small scripts that work together and I can attach them to Go’s and see if they work.?

bump

What is the error message?

Assets\match3code\code\abb.cs(8,2): error CS0246: The type or namespace name ‘myScript’ could not be found (are you missing a using directive or an assembly reference?)

myScript aaa;
Shouldn’t that be other way round? First type then variable name?

If this is the case of your error you should learn from it and do some proper type and variable naming in the future.

No its not supposed to be the other way round.I did it just to see and you get the error
Assets\match3code\code\abb.cs(12,14): error CS0029: Cannot implicitly convert type ‘aaa[ ]’ to ‘aaa’

Would you mind loading that code into a empty project and seeing if you get any errors?

Maybe you meant GetComponentInChildren instead of GetComponentsInChildren.

I don’t get it. The usual variable declaration in C# is type variable name. So I would assume it SHOULD be aaa myScript;

Yes.

Yes I did mean that but it makes no difference to change it,I even tried loading up unity 5 (still the best version imo) and using monodevelop in case it was an error in the vs code editor itself but it throws an error in that as well I don’t understand these scripts should work no problemo shouldn’t they?.

A google search will show every example has it myScript etc first.

That may be true when MyScript is a type (class or struct). In the line above you write GameObject test; so the type first and the name of your desired variable last. You do it the other way round and write your variable name “myScript” first and your type “aaa” last. Thats all I’m saying.

If you need a refresher look into C# Yellow book.

There is still the chance that I’m understanding your code or your intentions wrong. I have made my point and am out now. But my advise of proper naming conventions still applies. Good luck.

1 Like