Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheIronHobo · Jul 22, 2013 at 08:31 AM · gameobjectreference

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheIronHobo · Jul 22, 2013 at 07:56 PM 0
Share

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?

avatar image SpecticalPro · Jul 22, 2013 at 08:53 PM 0
Share

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

avatar image TheIronHobo · Jul 22, 2013 at 09:23 PM 0
Share

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...

avatar image SpecticalPro · Jul 22, 2013 at 09:33 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a List of references of instances in a static variable 1 Answer

Objects created in Editor script lose their reference after Play/Stop? 1 Answer

Setting an object reference to null 1 Answer

Can't re-enable a gameobject 1 Answer

Reference to Object error but script works fine 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges