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 DragonMind · Jan 26, 2014 at 03:21 PM · objectaccess

Access object from script problem.

I have found many suggestions using google, on how to access the object which your script is attached to, but common for all of them are: They do not work! Because it involves a ":" in the initialization, which Unity won't accept.

My question(s) are:

1a) Could this be a product of failure, due to the fact I use the free version?

1b) ...if not, can you access an object which the script is attached to, without using ":"?

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 robertbu · Jan 26, 2014 at 03:22 PM 1
Share

Your question does not make much sense. For any script, you can use 'gameObject' (small 'g') to access the game object the script is attached to. Please post your code to get a more accurate answer.

avatar image DragonMind · Jan 26, 2014 at 03:43 PM 0
Share

Yeah I have tried that too (gameObject, according to examples), but it doesn't work either. The question makes perfect sense, as the problem is written clearly. Not knowing the context because you know too little of the issue ...yes I admit that might give problems, but I can only redirect you to the various examples I found using google, as it is their code I used.

However I can tell you I want to access the objects X, Y and Z, through the script, that it is attached to.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Murkas · Jan 26, 2014 at 04:32 PM

Just like this (As an example):

 using UnityEngine;
 using System.Collections;
 
 public class YourScript : MonoBehaviour
 {
   void Start(){
     gameObject.transform.position=new Vector3(0.2f,0.5f,0.3f);
   }
 
   void Update(){
     float x = gameObject.transform.position.x;

     //do whatever you want with this x

     //since we can't change x directly, we have to do a workaround
     Vector3 LastPos = gameObject.transform.position;
     LastPos.x=x;
     gameObject.transform.position=LastPos;
   }
 }
Comment
Add comment · Show 9 · 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 DragonMind · Jan 26, 2014 at 06:02 PM 0
Share

I like your answer, but you set the variables, I need to retrieve them, and I did try gameObject.transform.x + variations of it, to no success.

avatar image Murkas · Jan 26, 2014 at 06:50 PM 0
Share

Okay, what exactly do you want to do?

  • Read the single variables (x,y,z) - Write the single variables (x,y,z) - In/Decrease the single variables (x,y,z)

avatar image DragonMind · Jan 26, 2014 at 08:08 PM 0
Share

I need to read them to assign them to other variables, then process those variables with a math-function(non-code), then assign that product back to the original variables.

avatar image Murkas · Jan 26, 2014 at 08:51 PM 0
Share

Okay, I changed my awnser. It works similar with y and z.

avatar image DragonMind · Jan 27, 2014 at 10:49 AM 0
Share

Thank you very much $$anonymous$$urkas, gonna try it out.

Just tried it out, Unity says it can't find gameObject. I really hope this is because I got the free version. UPDATE: Unity even gives errors NOW, for the script that comes with it, saying it can't find gameObject. This is getting sick, excuse me for my language.

Show more comments

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

19 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

Related Questions

A way to set object's script as a variable of sorts? 1 Answer

How can I access other scripts and their functions? 3 Answers

Having script accessing issues 2 Answers

Access variable from another script? Health! 3 Answers

How to make an object jump when it's clicked? 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