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 keitasama · Jun 21, 2012 at 09:52 PM · gameobjecttransform

How can I manipulate the script attached to a Transform Object?

I have been using a script that controls elements in the scene as Transform objects. Those objects have a javascript,and are within a GameObject.

I have found some trouble when I want to access their script, because what I have found, only works for GameObjects, not for Transform.

can you give me an idea howo to solve this? in code, or reoganizing my code

thanks

I have something like this

player script:

 var health = int;
 health=100;

enemy script:

  var target : Transform;    // I place my player here in the editor
 var playerHealth= GetComponent(CameraControlPlayer).health;
 if(playerHealth>10)
 atack();

on the enemy script, I am not able to read or manipulate the values or variables from player(Transform) script.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDarkVoid · Jun 21, 2012 at 09:57 PM

u need to do a

 public static var health = int;

in order to access the var.

Comment
Add comment · Show 1 · 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 keitasama · Jun 22, 2012 at 03:05 AM 0
Share

thanks it worked!

avatar image
0

Answer by Berenger · Jun 21, 2012 at 09:59 PM

That line

 var playerHealth= GetComponent(CameraControlPlayer).health;

is actually

 var playerHealth = this.GetComponent(CameraControlPlayer).health;

Which means it's going to return the component CameraControlPlayer attached to the same gameObject this script is attached to. So, it will return null. Try

 var playerHealth = target.GetComponent(CameraControlPlayer).health;
Comment
Add comment · Show 1 · 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 keitasama · Jun 22, 2012 at 03:05 AM 0
Share

I've try that, but the problem was not there, it was the static public var, that kingk614 commented. But I was hoping for more solutions to get more experience on the error. Thanks! =)

avatar image
0

Answer by keitasama · Jun 22, 2012 at 10:08 AM

@kingk614: yes that solved my problem. what I wrote at the end was: public static var health = int; health=100; enemy script:

var target : Transform; // I place my player here in the editor var playerHealth= target.GetComponent(CameraControlPlayer).health; if(playerHealth>10) atack();

Comment
Add comment · 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
0

Answer by TheDarkVoid · Jun 22, 2012 at 02:36 PM

also if you ever wanted a access more from that script you should do something like this:

 //C#
 public GameObject ScriptObject;
 public ScriptName SomeName;
 
 //in the update()
 SomeName = ScriptObject.GetComponent<ScriptName>();
 //u can now use the SomeName variable to access any 
 //public variable or function from ur script.


 //Java Script
 public ScriptObject : GameObject;
 public SomeName : ScriptName;
 
 //in the update()
 SomeName = ScriptObject.GetComponent(ScriptName);
 //u can now use the SomeName variable to access any public 
 //variable or function from ur script.

Hope this helps you.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to destroy a transform's parent object. 1 Answer

Attach Transform to another GameObject 0 Answers

guiText/GameObject position 2 Answers

How To Move GameObjects In Fixed Path? 1 Answer

C# Function Transform to Gameobject Convert 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