- Home /
The name `gameObject' does not exist...? Yes i did my research.
TTA_Coord.cs(12,15): error CS0103: The name `gameObject' does not exist in the current context
Please help me make this disappear. I have looked this up and have not been able to apply the solutions to my code sucessfully. I am working on a unique project and I need a script attached to a sprite that references a non Mono behaviour scripts "X Y and Z" integers. Here is the script (walker) thats supposed to do the referencing.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Walker : MonoBehaviour {
//Storing the reference to RagePixelSprite -component
private IRagePixel ragePixel;
//enum for character state
public enum WalkingState {Standing=0, WalkRight, WalkLeft};
public WalkingState state = WalkingState.Standing;
//walking speed (pixels per second)
GameObject coordPosit;
void Start () {
ragePixel = GetComponent<RagePixelSprite>();
coordPosit= GameObject.Find("TTA_Coord").GetComponent<TTA_Coord> ();
}
void Update () {
TTA_Coord coordPos = coordPosit.GetComponent<TTA_Coord>();
//Check the keyboard state and set the character state accordingly
if (coordPos.x == 5 && coordPos.y == 5 && coordPos.z == 0)
{
state = WalkingState.WalkLeft;
}
else if (coordPos.x == 5 && coordPos.y == 6 && coordPos.z == 0)
{
state = WalkingState.WalkRight;
}
else
{
state = WalkingState.Standing;
}
switch (state)
{
case(WalkingState.Standing):
ragePixel.SetSprite("Dripper", 0);
//Reset the horizontal flip for clarity
ragePixel.SetHorizontalFlip(false);
ragePixel.PlayNamedAnimation("drip", false);
break;
case (WalkingState.WalkLeft):
ragePixel.SetSprite("Dripper", 0);
//Flip horizontally. Our animation is drawn to walk right.
ragePixel.SetHorizontalFlip(true);
//PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
ragePixel.PlayNamedAnimation("drip", false);
//Move direction. X grows right so left is -1.
break;
case (WalkingState.WalkRight):
ragePixel.SetSprite("Barred", 0);
//Not flipping horizontally. Our animation is drawn to walk right.
ragePixel.SetHorizontalFlip(false);
//PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
ragePixel.PlayNamedAnimation("there", false);
//Move direction. X grows right so left is +1.
break;
}
//Move the sprite into moveDirection at walkingSpeed pixels/sec
}
}
And here's the script (TTA_Coord) thats supposed to be referenced...
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TTA_Coord
{
void Start () {
fbs = gameObject.AddComponent<Walker>();
}
public Walker fbs;
public int x = 0;
public int y = 0;
public int z = 0;
public override string ToString ()
{
return x + "," + y + "," + z;
}
public TTA_Coord(){}
public TTA_Coord(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
public bool IsValid(){
return (this.x >= 0 && this.y >= 0 && this.z >= 0);
}
public TTA_Coord(TTA_Coord source, TTA_CommandType command){
if(source == null){
return;
}
this.x = source.x;
this.y = source.y;
this.z = source.z;
switch(command){
case TTA_CommandType.North:{
this.y -= 1;
break;
}
case TTA_CommandType.South:{
this.y += 1;
break;
}
case TTA_CommandType.East:{
this.x +=1;
break;
}
case TTA_CommandType.West:{
this.x -=1;
break;
}
case TTA_CommandType.Up:{
this.z += 1;
break;
}
case TTA_CommandType.Down:{
this.z -=1;
break;
}
}
}
public TTA_Coord(TTA_Coord source){
if(source == null){
return;
}
this.x = source.x;
this.y = source.y;
this.z = source.z;
}
public override bool Equals (object obj)
{
if(!(obj is TTA_Coord)){
return false;
}
TTA_Coord comp = (TTA_Coord)obj;
if(comp.x == x &&
comp.y == y &&
comp.z == z){
return true;
}
return false;
}
public override int GetHashCode ()
{
return x + (10*y) + (100*z);
}
}
Any help would be gladly appreciated. Thanks bunches. --TheIronHobo
Answer by hatuf · Jul 22, 2013 at 08:40 AM
TTA_Coord doesn't inherit from MonoBehavior so it doesn't have a gameObject reference. Or a automatic call to Start() by the way. If you want to reference the gameObject from TTA_Coord you have to pass the instance in, maybe in the constructor.
Hey, thanks for the reply. I've been trying to implement your solution and I am having a hard time. Could you possibly show me an example?
TTA_Coord doesn't know the gameObject exists, hatuf is saying that you need to pass the gameobject from walker to TTA_Coord. To do this you would need a method like init() or something like that, which you can call from walker and pass the game object to it ie. coordposit.init(gameObject);
then in TTA_Coord you can have..
public void init(GameObject go){ this.go = go; go.AddComponent(); }
be sure to remove the line from inside your start method.
Alternatively you can just make TTA_Coord inherit from $$anonymous$$onoBehaviour like so
public class TTA_Coord : $$anonymous$$onoBehaviour
Oh thank you! A soul to save this question. Thanks for your answer. Your solution looks great but my limited program$$anonymous$$g skill does not comprehend what "Go" should be. and unfortunately I cannot have TTA_Coord inherit from $$anonymous$$onobehviour...
go is just a variable you will declare inside of the TTA_Coord class just like your
public Walker fps
you can have it either being public or private, preferable private.
it will be type of GameObject so the declaring line would look like..
private GameObject go
but really, if you are only using this gameobject in the init function you don't need it at all and can something like this..
public void init(GameObject go){
fbs = go.AddComponent()≶Walker>;
}
but I'm not entirely sure you can do that in one line, so if not this would work aswell..
public void init(GameObject go){
go.AddCompnent(); fbs = go.getComponent("walker");
}
Im a little rusty but this should work...
and go is just a short term i use for GameObject, sorry for the confusion.