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 mhlongley88 · Jan 08, 2014 at 02:00 AM · transformpositionspawntransform.positionstatic variable

getting the transform.position of a public static Transform?

Any help would be grand!

I am trying to recieve the transform.position of a global Transform for a Spawn function.

Here's what I am working with so far...

 public class Creature_Movement : MonoBehaviour
 
 {
 
 public static Transform spawnPointForPlayer1;
 
 public static void Spawn()
 
 {
 
 // reset the character's speed
 
 movement.verticalSpeed = 0.0f;
 
 movement.speed = 0.0f;
 
 
 
 // reset the character's position to the spawnPoint
 
 transform.position = Creature_Movement.spawnPointForPlayer1.transform.position;
 
 
 
 }
 
 }
 
Comment
Add comment · Show 2
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 ncallaway · Jan 08, 2014 at 02:13 AM 0
Share

What is the problem that you're running into? Are you getting a NullPointerException, or some other problem?

avatar image mhlongley88 · Jan 08, 2014 at 03:43 AM 0
Share

There's 3 error messages.

Assets/Scripts/Creature$$anonymous$$ovement.cs(330,17): error CS0120: An object reference is required to access non-static member Creature_$$anonymous$$ovement.movement' Assets/_Scripts/Creature_$$anonymous$$ovement.cs(332,17): error CS0120: An object reference is required to access non-static member Creature_$$anonymous$$ovement.movement'

Assets/Scripts/Creature$$anonymous$$ovement.cs(338,17): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform'

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jan 08, 2014 at 02:22 AM

So, what's the problem? Although somewhat weird, this code should work. Static variables don't appear in the Inspector - is this the problem? If so, you must assign the variable at runtime - maybe with a code like this:

 void Start(){
   // find the empty object named "PlayerSpawnPoint":
   spawnPointForPlayer1 = GameObject.Find("PlayerSpawnPoint").transform;
 }

But I wouldn't use static functions and variables in this case - you will have trouble if more than one instance of this script exists, since a static variable is shared by all instances.

Comment
Add comment · Show 2 · 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 mhlongley88 · Jan 08, 2014 at 03:52 AM 0
Share

Thanks aldonaletto!

I tried placing the GameObject.Find in the start function, ad I still seem to get the same error message.

Assets/Scripts/Creature$$anonymous$$ovement.cs(330,17): error CS0120: An object reference is required to access non-static member Creature_$$anonymous$$ovement.movement' Assets/_Scripts/Creature_$$anonymous$$ovement.cs(332,17): error CS0120: An object reference is required to access non-static member Creature_$$anonymous$$ovement.movement'

Assets/Scripts/Creature$$anonymous$$ovement.cs(338,17): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform'

The script is specific to 1 of 3 players, so there will only be running 1 instance of the script at a time.

Thanks for all the help!

avatar image aldonaletto · Jan 08, 2014 at 12:32 PM 1
Share

The problem is in the movement variable: a static function (Spawn, in this case) can refer only to local or static variables. You could declare movement static to solve the problem, but again I think that this isn't the best approach: the correct way would be to remove the static keyword from Spawn and the other variables.

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

20 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 avatar image avatar image avatar image

Related Questions

Setting Position of Spawned Prefab 2 Answers

How to find a transform with name in prefab 2 Answers

Place object randomly at any predefined spawn point 1 Answer

Getting Transform position using mouse position 3 Answers

Need instantiated objects to spawn at transform NOT at origin?(Solved) 2 Answers


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