Hello,about GameObject.Find() and Script

It’s code:

 public string Object_Name = "qiang_danjia_on";  //"qiang_danja_on" is name of script file.

 GameObject ObjectTran;


void Start () {


 ObjectTran = GameObject.Find("qiang_danjia_on");   //It's GO!
 ObjectTran = GameObject.Find(Object_Name);        //can't work

        
	
	}

The error message:
NullReferenceException
Component_Move.Update () (at Assets\MyScript\Component_Move.cs:41)

What is the reason?
GameObject name and script name can not be used as a parameter to pass?

Thank all :slight_smile:

All works fine for me. Check the example (C# code).

I’m using Unity 2.1 on Mac OS X 10.4.11 (Tiger).

Let me know if it works !!!

174115–6240–$example_635.zip (472 KB)

:slight_smile: :slight_smile: :slight_smile: Thank you!I see.It must be type private :slight_smile:

I still have a problem.

private string Object_Name= "qiang_danjia_on";

private string Component_Name = "Move_DanJaKou";

GameObject ObjectTran;
void Update()
{


 ObjectTran = GameObject.Find(Object_Name);
            ((Move_DanJaKou)ObjectTran.GetComponent(typeof(Move_DanJaKou))).a = 20;    //It's can go
            ((Move_DanJaKou)ObjectTran.GetComponent(Component_Name)).a = 20;    //It's can go

((typeof(Component_Name))ObjectTran.GetComponent(Component_Name)).a = 20;    //It's display error #1

}

#1 message: error CS0118: Component_Move.Component_Name' is a field’ but a `type’ was expected


How this should be resolved?
or
The contents of string can’t be used as script type to use it?

Thank you

Well, typeof operator is from language itself so is not from Unity.

Here you can find the specification for C# and here the ones for javascript.

As you can see the typeof operator accept only a class type and not string. You cannot do

((typeof(Component_Name))ObjectTran.GetComponent(Component_Name)).a = 20;

because Component_Name is a string and not a type defined in your project and this is the reason of the error you get: Component_Move.Component_Name' is a field’ but a `type’ was expected.

However my question is: why have you to make a parameter for a type?
Why don’t you use an abstract class that could be then extended from different ones so you can have common operations redefined as much as you like?

Another suggestion is to not use the gameObject.Find because it’s a lot expensive. If you need the reference to a prefab that is not instantiated you can give the reference direcly after instantiation to the class who made the instance of the prefab.

To make you understand better what I’m saying, take a look to the new example. You will find 2 scenes: the first is the use of different component by using abstract class and its overrides, the second one is the use of a player and enemy hierarchy WITHOUT USING ANY GameObject.Find to optimaze the execution !!!
If you want you can also combine these 2 solutions to have one really optimized project !!!

Enjoy !!! :stuck_out_tongue:

174513–6258–$example_416.zip (525 KB)

:slight_smile:
I can use Javascript achieve this functionality ,so i think C # can also achieve this feature

The following is my completion of the Javascript Code in yesterday

174864–6268–$myscripttry_207.rar (869 KB)

The reason why we are so write,Because they can reduce the complexity of upgrading.
When adding a new gameobject,Do not need to modify other object.Only modify their own script code (Only set GameObject name and script name and to pass to Script_Center script ,It’s go! ),This can be achieved Uniformity and flexibility.

But,I like C# ,No like javascript. :sweat_smile:

I see… but the operation you need to do are manual while if you use abstract class and references all will become automatic :wink:

However it’s a subjective choice linked to your programming style !!!

:smile: :smile: :smile:
Very good!
I am succeeded.

using UnityEngine;
using System.Collections;

public class Chief_Script : MonoBehaviour
{

    public bool ON=false;

	// Use this for initialization
    public string GO_name;

    public string Script_name;

	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

        if (ON) 
        {

//            ((MyClass)GameObject.Find(GO_name).GetComponent("MyClass")).move=true;    //It can go     
            ((MyClass)GameObject.Find(GO_name).GetComponent(Script_name)).move = true;
            ON = false;
        }

	}
}

Thank you very much for really !! Apache :smile: :smile:

174956–6272–$myscripttrycsharp_248.rar (865 KB)

You’re welcome !!! :wink: