SmothFollow script on a newly instantiated object.

238 views
Skip to first unread message

Paul

unread,
Oct 18, 2012, 11:01:31 AM10/18/12
to unity3d...@googlegroups.com
Hi folks,
 
I have a piece of code where I instantiate a new object from a prefab and then I want to set the target of it's smooth follow script. But I keep getting an error
 
the type or namespace name `SmoothFollow' could not be found. Are you missing a using directive or an assembly reference?
 
A search seems to point in the direction that the smooth follow script has to be in the Standard Assets folder, it is, although a sub folder of it. I did try and move it to the top level, but made no difference.
 
here's the piece of code:
 
 void OnTriggerEnter(Collider otherCollider)
 {
  Instantiate (NewPiece, new Vector3(transform.position.x-5,transform.position.y,transform.position.z),transform.rotation);
  NewPiece.name = "Piece";
  NewPiece.GetComponent<SmoothFollow>().Target = m_lastPiece;
    
  m_lastPiece = NewPiece;
  Destroy (otherCollider.gameObject);
  
 }

The GetComponent line is where I am getting the error.
 
Also the obect I have collided with, otherCollider, has a public int declared in a script attached to it. Can I find what this is within the above function?
 
thanks
Paul
 
P.S. have tried the GetComponent with and without the () after SmoothFollw, no difference.
P.P.S. sorry if this is a realy daft question, just getting started :)

Mark Aherne

unread,
Oct 18, 2012, 11:16:56 AM10/18/12
to unity3d...@googlegroups.com
Hey Paul,

No problems.

That code is looking for a SmoothFollow component (script) on the NewPiece GameObject.

The SmoothFollow script is one that comes with Unity, it's in the Standard Assets bundle.

This script has a Target, when it's set the GameObject that it's on will follow the Target.

All the best,

Mark

Sergio Calabria

unread,
Oct 18, 2012, 11:26:05 AM10/18/12
to unity3d...@googlegroups.com

If smoothfollow is on unity script, and your script is I'm C#, you should probably avoid using the generics <component> form, try with GetComponent("compoment") instead...

Mark Aherne

unread,
Oct 18, 2012, 11:28:21 AM10/18/12
to unity3d...@googlegroups.com
You can call a UnityScript / JavaScript script using generics in C#, that's fine.

Paul Doyle

unread,
Oct 18, 2012, 11:33:57 AM10/18/12
to unity3d...@googlegroups.com
Hi Mark
 
Thanks for the quick reply. There is a smooth follow script on the object, or at least I can see it in the inspector during the game play
 
Attached screenshot
 
Paul
Image1.jpg

Mark Aherne

unread,
Oct 18, 2012, 11:50:47 AM10/18/12
to unity3d...@googlegroups.com
Ah I see, I missed something.

You are cloning NewPiece, but not using GetComponent on the clone.

Try this (not tested):

void OnTriggerEnter(Collider otherCollider)
 {
  GameObject newPieceClone = (GameObject)Instantiate (NewPiece, new Vector3(transform.position.x-5,transform.position.y,transform.position.z),transform.rotation);
  
newPieceClone.name = "Piece";
  
newPieceClone.GetComponent<SmoothFollow>().Target = m_lastPiece;
    
  m_lastPiece = 
newPieceClone;
  Destroy (otherCollider.gameObject);
  
 }

Paul Doyle

unread,
Oct 18, 2012, 12:12:37 PM10/18/12
to unity3d...@googlegroups.com
Argh. Thanks. So obvious when I see it. Will try in the morning 

Cheers Paul. 

Sent from my iPhone

Paul Doyle

unread,
Oct 19, 2012, 10:00:31 AM10/19/12
to unity3d...@googlegroups.com
Mark
 
afraid to report exact same error unfortunately
 
regards
Paul

Sergio Calabria

unread,
Oct 19, 2012, 11:34:45 AM10/19/12
to unity3d...@googlegroups.com

Hi Paul,

Make sure only one script is in standard assets or a folder called plugins, the script you're referencing in getComponent. So that script will compile before you try and access it with GetComponent in the other script, put your script in root assets folder.

http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html

As much as it's less effective, if that still not working, you could try with
GetComponent("scriptName")

Mixing can be a pain, you might want to try and recode the script in CSharp.

Good luck,
Sergio.

Paul Doyle

unread,
Oct 19, 2012, 3:57:26 PM10/19/12
to unity3d...@googlegroups.com
Thanks Sergio
 
Re-coded into C# and now can access the script. Now I have a whole new load of problems!! lol.
 
Paul

Reply all
Reply to author
Forward
0 new messages