• 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 Matthew0123 · Jan 02, 2013 at 10:36 PM · javascriptgameobjectvariables

Assigning variables to GameObjects

I am making a script to call variables from the object which hits the targets to calculate force. But, I was wondering if there was a simple way to make a private variable to that GameObject and later to be able to access it like you can do with transform.

In tranform you can do

terrain.transform or this.transform I want to be able to access a variable like that be able to do. var bulletWeight : float = GameObject.Find("Bullet").weight; bullet.mass etc.

Is there a way to do this? Without making a script to assign the value and then accessing that script value and calling it from there? I rather not do it this way unless I have too.

Comment
Add comment · Show 3
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 Next Beat Games · Jan 02, 2013 at 10:41 PM 1
Share

quick note. I wouldn't ever use GameObject.Find("") if I were you. Especially for objects like bullets which quickly spawn and are destroyed. $$anonymous$$ake sure you keep references to objects like bullets. Or even use a circular queue of say 10 blank bullet references.

avatar image Matthew0123 · Jan 02, 2013 at 10:44 PM 0
Share

Next Beat, how would you do this? I am still new to UnityScript and trying to learn so I have minimal knowledge of this.

avatar image Next Beat Games · Jan 02, 2013 at 10:54 PM 0
Share

ok i will try to explain with pseudo code:

class CBullet { Vector3 startPos; Vector3 endPos; }

// In your script you wish to use bullets in:

CBullet[] bullets[10]; // 10 = max bullets used by this transform

int queuePosBegin = 0;

int queuePosEnd = 0;

public fire() { bullets[++queuePosEnd].startPos = gun$$anonymous$$uzzlePos; bullets[queuePosEnd].endPos = gun$$anonymous$$uzzlePos+(gun$$anonymous$$uzzle.Forward * someConstant); }

OnBulletHit() { ++queuePosBegin; }

2 Replies

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

Answer by Piflik · Jan 02, 2013 at 10:41 PM

You cannot access private variables directly from other scripts at all. You would need getter/setter functions...

Public variables can be accessed by:

 gameObject.GetComponent(ScriptName).VariableName;    //UnityScript
 //or
 gameObject.GetComponent<ScriptName>().VariableName;    //C#

Also, GameObject.Find() should only ever be called in Start() or Awake(). Don't call it repeatedly, since it is quite expensive.

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 jogo13 · Jan 02, 2013 at 10:44 PM

You'll have to do it via attached script classes (which can be attached as 'components.') Even the basic Gameobject is class that is a collection of other classes and structs (such as transform.)

Simple class example (c#)

 using UnityEngine;
 using System.Collections;
 
 public class bullet : MonoBehaviour {
  
        float weight;
        float speed;
 
 }
 
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 Matthew0123 · Jan 02, 2013 at 10:45 PM 0
Share

Oh okay, thanks for the explanation.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

exspecting EOF, found '}'. 3 Answers

A node in a childnode? 1 Answer

Question Concerning transform.Rotate 1 Answer

Making changes to a terrian in game 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges