Cannot seem to get a reference to a script component to work, despite many different attempts and a lot of work

Updated with latest code as per aldonoletto’s instructions…

Current errors:

Assets/Scripts/invaderMovement.cs(16,97): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Assets/Scripts/invaderMovement.cs(16,84): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)’ has some invalid arguments

Assets/Scripts/invaderMovement.cs(16,84): error CS1503: Argument #1' cannot convert object’ expression to type `System.Type’

I really appreciate this and hope it puts more than just me out of their misery in the future!

=================

Hi there,

I’ve spent about 6 hours on this problem, so before anyone flames me, says RTFM (or similar), or closes the thread, hear me out.

I have read MANY posts (over 30), spent a LONG time looking at similar questions and watching videos but cannot get this to work. I have read the documentation over countless times and it is NOT helping, so please just don’t just post a link to the manual and/or scripting reference for GetComponent or whatever.

Everything that I’ve tried has given me an error. You can see that I’ve done this from scratch myself so I am NOT lazy, I am just learning.

I’m making a simple Space Invaders game, and I have one script for the invaders movement (below) and another script on an empty gameObject (invadersControllerObject) called invaderController.

Inside invaderController there are 3 static public booleans (movingLeft, movingDown, InvadersCanMove).

The code in the invaderMovement script (below) uses these to determine how the invaders should move. The reason I did it this way is so that all aliens move synchronously as a block (i.e. when one invader reaches the margin then they ALL move down one line and head back to the other side of the screen, and do it in ‘steps’ rather than smoothly).

I have had all types of errors, but the current one is:

“Assets/Scripts/invaderMovement.cs(53,60): error CS0176: Static member `UnityEngine.GameObject.Find(string)’ cannot be accessed with an instance reference, qualify it with a type name instead”

…and similar when it encounters a call on a boolean.

I am obviously missing something simple here, but I can’t find it. Is anyone able and willing to help me?

Many thanks, code below.

using UnityEngine;
using System.Collections;

public class invaderMovement : MonoBehaviour {

//Time between Invader 'steps'
public float invaderSpeed;
//How far invaders move left or right
public float invaderMoveStep;
//How far invaders move vertically
public float invaderVertStep;
//Margin for invaders to keep from the edge of the screen
public float invaderMargin;

//Establishing connection to invaderController Script on invaderControllerObject for use in co-routine.
invaderController invaderCont = GameObject.Find("invaderControllerObject").GetComponent(invaderController);
	

// Use this for initialization
void Start () {
	
		
}

// Update is called once per frame
void Update () {
	
	//If InvadersCanMove is true, the InvaderMove function will be called. 
	//InvadersCanMove is set to false to prevent multiple firings of the InvaderMove function
	if (invaderController.InvadersCanMove){
		StartCoroutine ("InvaderMove");
		invaderController.InvadersCanMove = false;			
	}
	
		
}

//Function to move invaders. Called within update.
//It's not a function technically, it's a co-routine. This is so I can use the WaitForSeconds command.
//InvadersCanMove is reset to true so that the InvaderMove function can be called again.

IEnumerator InvaderMove (){
	
	//Convert relative position of invader into 'proper' screen coordinates and assign it to a new construct - invaderPos
	Vector3 invaderPos = Camera.main.WorldToScreenPoint(transform.position);

	
					
	//Checks to see if it's time to move down
	//If it is, moves down one step, then tests which margin it's at.
	//Moves one step out of margin and sets direction for travel
	//If it's NOT time to move down, are we travelling left or right?
	//Moves accordingly by one step.
				
	yield return new WaitForSeconds(invaderSpeed);
	
	if (invaderCont.movingDown) {
			transform.Translate (Vector2.up * -invaderVertStep);

		//Stop invader moving down after this step
		invaderCont.movingDown = false;
		
		//If I don't move it out of the margin then invader keeps going down due to Update loop check.
		//This should move it one step from margin
		//Right margin
		if (invaderPos.x > (Screen.width-invaderMargin)){
			yield return new WaitForSeconds(invaderSpeed);
			transform.Translate (Vector2.right * -invaderMoveStep);
			invaderCont.movingLeft = true;
		}
		//Left margin
		if (invaderPos.x < invaderMargin){
			yield return new WaitForSeconds(invaderSpeed);	
			transform.Translate (Vector2.right * invaderMoveStep);
			invaderCont.movingLeft = false;
		}
	} else {
		if (invaderCont.movingLeft){			
			transform.Translate (Vector2.right * -invaderMoveStep);
		} else {
			transform.Translate (Vector2.right * invaderMoveStep);
		}
	}
	
	//Reset variable for Update to fire again.
	invaderCont.InvadersCanMove = true;
	
	//If it's in the margins, it's time to move it down a row.
	//Right margin
	if (invaderPos.x > (Screen.width-invaderMargin)){
		if (!invaderCont.movingLeft){
			invaderCont.movingDown = true;	
		}
		
	}
	//Left margin
	if (invaderPos.x < invaderMargin){
		if (invaderCont.movingLeft){
			invaderCont.movingDown = true;
		}
	}
}

}

You’re using gameObject, which is a reference to the GameObject to which this script is attached, and should instead use GameObject, which is the GameObject class name:

invaderController invaderCont = GameObject.Find("invaderControllerObject").GetComponent(invaderController);

SIDENOTE: searching for an object by name is a slow operation - it would be much better to find the object and its script only once at Start:

invaderController invaderCont; // declare this variable outside any function

void Start(){
  // assign to it only once at Start:
  invaderCont = GameObject.Find("invaderControllerObject").GetComponent(invaderController);
}

Well I would like not to but RTFM since gameObject.Find should be GameObject.Find line 48.